How to check if a URL is localhost in TypeScript

1 Answer

0 votes
function isLocalhost(url : string) : boolean {
  	return url.includes('localhost') || url.includes('127.0.0.1');
}

console.log(isLocalhost('http://localhost:8000/'));

console.log(isLocalhost('http://127.0.0.1:1234/test/'));

console.log(isLocalhost('http://commentsinfo.com'));




  
/*
run:
  
true
true
false
  
*/

 



answered Apr 28, 2022 by avibootz

Related questions

1 answer 150 views
1 answer 169 views
1 answer 150 views
1 answer 119 views
1 answer 66 views
1 answer 77 views
77 views asked Jan 31, 2025 by avibootz
...