如何检查URL字符串是否包含2个子字符串中的任何一个?



如何检查字符串是否包含其他2个字符串中的任何一个?例如,我想检查URL是否包含以下字符串localhost10.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
}

您可以使用.some()来确定字符串是否包含某些关键字。

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
}

相关内容

最新更新