How can I check if a string contains either one of 2 other strings? For example, I want to check if a URL contains either one of these string localhost or 10.0.2.2
http://localhost:5000 -> true
10.0.2.2:5000 -> true
dasdasdasdasdlocalhostdasdasd -> true
dasdasdasd10.0.2.2:5000dasdasd -> true
http://example.com -> false
if (/localhost|10.0.2.2/.test(URL)) {
//your code
}
You could use .some() to determine if a string contains some of the keywords.
const urls = ['10.0.2.2:5000', 'dasdasdasdasdlocalhostdasdasd', 'dasdasdasd10.0.2.2:5000dasdasd', 'http://example.com'];
const keywords = ['localhost', '10.0.2.2'];
const urlsWithKeywords = urls.filter(url => {
return keywords.some(keyword => url.includes(keyword));
});
console.log(urlsWithKeywords);
var pattern = /localhost|10.0.2.2/;
var url= "your url here";
if (pattern.test(url)) {
// That means return true
//your rest of the code
}