如何检查字符串是否包含JavaScript中字符串数组的子字符串

时间:2021-06-21 01:35:10

I'm still learning JavaScript but I just can't seem to find a way to see if a string contains a substring.

我还在学习JavaScript,但我似乎无法找到一种方法来查看字符串是否包含子字符串。

Basically I have some Titles of some individuals and I want to see if the Title contains strings like "President" or "Sr" in the title this is what i have so far but does not seem to be working.

基本上我有一些个人的标题,我想看看标题是否包含标题中的“总统”或“高级”字符串,这是我到目前为止所做的,但似乎没有起作用。

var title = "President of Sales";
var arrayOfTitles = ["President","Chief","VP","SVP","Director","Manager","Mrg","Sr","Senior","Executive Assistant","Principle Architect","GM","Technical Advisor"];

var re = new RegExp(arrayOfTitles.join("|"),"i");

for(i = 0; i < arrayOfTitles.length; i++){
        if ( re.test(gr.title)){
            return; 
                }
     }

However this code will not work with String likes "Jr VP" or "President of Sales". Is there a way to build an array of Regex of these strings?

但是,此代码不适用于字符串,如“Jr VP”或“销售总裁”。有没有办法构建这些字符串的正则表达式数组?

any help would be great thanks

任何帮助都会非常感谢

3 个解决方案

#1


1  

You do not need to run a loop

您不需要运行循环

var title = "President of Sales";
var arrayOfTitles = ["President","Chief","VP","SVP","Director","Manager","Mrg","Sr","Senior","Executive Assistant","Principle Architect","GM","Technical Advisor"];

var regex = new RegExp(arrayOfTitles.join("|"), "i");
//The regex will return true if found in the array
if ( regex.test(title) ){
  console.log("has");
}

#2


1  

How about something simple like :

简单的事情怎么样:

var title = "President of Sales";
var arrayOfTitles = ["President","Chief","VP","SVP","Director","Manager","Mrg","Sr","Senior","Executive Assistant","Principle Architect","GM","Technical Advisor"];
var matches = (function() {
  for(var i=0; i < arrayOfTitles.length; i++) {
	if(title.indexOf(arrayOfTitles[i]) > -1) { return true; }
  }
  return false;
}());

#3


0  

You can also use the includes() function, instead of indexOf().

您还可以使用includes()函数,而不是indexOf()。

     var title = "President of Sales";
       var arrayOfTitles = ["President","Chief","VP","SVP","Director","Manager","Mrg","Sr","Senior","Executive Assistant","Principle Architect","GM","Technical Advisor"];
       var matches = (function() {
         for(var i=0; i < arrayOfTitles.length; i++) {
               if(title.includes(arrayOfTitles[i])) { return true; }
         }
         return false;
       }());

#1


1  

You do not need to run a loop

您不需要运行循环

var title = "President of Sales";
var arrayOfTitles = ["President","Chief","VP","SVP","Director","Manager","Mrg","Sr","Senior","Executive Assistant","Principle Architect","GM","Technical Advisor"];

var regex = new RegExp(arrayOfTitles.join("|"), "i");
//The regex will return true if found in the array
if ( regex.test(title) ){
  console.log("has");
}

#2


1  

How about something simple like :

简单的事情怎么样:

var title = "President of Sales";
var arrayOfTitles = ["President","Chief","VP","SVP","Director","Manager","Mrg","Sr","Senior","Executive Assistant","Principle Architect","GM","Technical Advisor"];
var matches = (function() {
  for(var i=0; i < arrayOfTitles.length; i++) {
	if(title.indexOf(arrayOfTitles[i]) > -1) { return true; }
  }
  return false;
}());

#3


0  

You can also use the includes() function, instead of indexOf().

您还可以使用includes()函数,而不是indexOf()。

     var title = "President of Sales";
       var arrayOfTitles = ["President","Chief","VP","SVP","Director","Manager","Mrg","Sr","Senior","Executive Assistant","Principle Architect","GM","Technical Advisor"];
       var matches = (function() {
         for(var i=0; i < arrayOfTitles.length; i++) {
               if(title.includes(arrayOfTitles[i])) { return true; }
         }
         return false;
       }());