Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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?

Thank you so much!

share|improve this question
Can you show how you would do it with jQuery? That would help people understand what you need help with writing a different way. – mrk Jul 8 '11 at 19:53
add comment (requires an account with 50 reputation)

marked as duplicate by Vivin Paliath, vcsjones, Paul Sonier, Chandu, Tim Schmelter Jul 8 '11 at 19:56

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

2 Answers

up vote 5 down vote accepted

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

share|improve this answer
What if the string starts with "World"? The operator should probably be >= 0. – mqchen Jul 8 '11 at 19:55
If I understand correctly, and >=0 value is a match – Eddie Jul 8 '11 at 19:56
add comment (requires an account with 50 reputation)

Check this out, first result on Stackoverflow: JavaScript: string contains

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

share|improve this answer
add comment (requires an account with 50 reputation)

Not the answer you're looking for? Browse other questions tagged or ask your own question.