This question already has answers here:
In javascript, how do you search an array for a substring match
                                
                                    (14个回答)
                                
                        
                        
                            Javascript Searching Array for partial string
                                
                                    (2个答案)
                                
                        
                        
                            Check for Partial Match in an Array
                                
                                    (5个答案)
                                
                        
                2个月前关闭。
            
        

如何使用Javascript在字符串数组中找到匹配项?例如:

var str = "https://exmaple.com/u/xxxx?xx=x";
var filter = ["/u","/p"];
if (!str.includes(filter)){
  return 1;
}


在上面的代码中,如果要匹配数组str的这两个值,我想搜索var filter

最佳答案

这应该工作:



var str = "https://exmaple.com/u/xxxx?xx=x";
var filters = ["/u","/p"];

for (const filter of filters) {
  if (str.includes(filter)) {
    console.log('matching filter:', filter);
    break; // return 1; if needed
  }
}

07-24 09:47
查看更多