Check if a string has a certain piece of text

Possible Duplicates:
Check if text is in a string
JavaScript: string contains

I'm trying to check if a string I import into my application has a certain piece of text. I know how to do this with jQuery, but how do I do it with straight up JavaScript?


Here you go:

var test = 'Hello World';
if( test.indexOf('World') >= 0){
  // Found world
}

More information on it can be found here:

http://www.w3schools.com/jsref/jsref_IndexOf.asp


Check this out, first result on Stackoverflow: How to check whether a string contains a substring in JavaScript?

var s = "foo";
alert(s.indexOf("oo") != -1);

indexOf checks at what position in the string is, and if it is not found -1 is returned

链接地址: http://www.djcxy.com/p/2040.html

上一篇: Node.js:String包含另一个字符串

下一篇: 检查一个字符串是否有一段文字