JQuerystring包含检查

我需要检查一个string是否包含另一个string?

var str1 = "ABCDEFGHIJKLMNOP"; var str2 = "DEFG"; 

我用什么函数来查明str1是否包含str2?

你可以使用javascript的indexOf函数。

 var str1 = "ABCDEFGHIJKLMNOP"; var str2 = "DEFG"; if(str1.indexOf(str2) != -1){ console.log(str2 + " found"); } 
 var str1 = "ABCDEFGHIJKLMNOP"; var str2 = "DEFG"; sttr1.search(str2); 

它将返回匹配的位置,如果没有find则返回-1。

检查JavaScript的indexOf函数。 http://www.w3schools.com/jsref/jsref_IndexOf.asp

请尝试:

 str1.contains(str2) 

如果您担心大小写敏感的情况下更改并比较string。

  if (stringvalue.toLocaleLowerCase().indexOf("mytexttocompare")!=-1) { alert("found"); } 

只需在foreach循环中就可以比较,分隔到string

 var commaseparateMainstring = 'shekar,jagadeesh,pavan,sai,suneel'; var sustring = 'shekar,gopi,raju,suneel'; //adding comma both sides of main string var main = ',' + commaseparateMainstring + ','; //substring change as array var substingArray = listToArray(sustring, ','); $.each(substingArray, function (index, value) { //calling is exist method here value means each value in substring var isexisted = isExist(main, value) if (isexisted) { //substring item existed } else { } }); //isExist method it checks substring exist in mainstring or not function isExist(mainString, substring) { if (mainString.indexOf(substring) != -1) { return true; } else { return false; } } 

这是为我工作

 if (~str.indexOf("Yes")) 

我用,

var text = "some/String"; text.includes("/") <-- returns bool; true if "/" exists in string, false otherwise.