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 159 views
1 answer 181 views
1 answer 157 views
1 answer 126 views
1 answer 72 views
1 answer 84 views
84 views asked Jan 31, 2025 by avibootz
...