How to check if string is present in variable in jquery

RegExp option...just because..RegExp.

var pattern = /run/;

//returns true or false...
var exists = pattern.test(myVar);

if (exists) {
  //true statement, do whatever
} else {
  //false statement..do whatever
}

You would use the Javascript method .indexOf() to do this. If you're trying to test whether the text of a DOM element contains the pattern, you would use this:

if($(myVar).text().indexOf(pattern) != -1)
    return true;

If the variable myVar isn't a selector string, you shouldn't wrap it in the jQuery function, though. Instead, you would use this:

if(myVar.indexOf(pattern) != -1)
    return true;

Tags:

Javascript