Reputation: 869
I'm creating a page search function and I need to compare the contents of one variable to see if it exists in another variable in an if statement.
To make it simple here's an expample:
var name = 'james';
var place = 'james lives in europe';
if (place:contains(james)) {
....... do something
}
Anyone know how to do this with jquery. cheers
Upvotes: 0
Views: 285
Reputation: 52789
You can use simple javascript -
if (place.indexOf(name) != -1) {
alert('do something');
}
Upvotes: 0
Reputation: 2945
Yes you can use a regexp :
var str="james lives in europe";
var reg1=new RegExp("[james]","g");
if (str.match(reg1)) {
document.write("'[james]' is in str");
}
Upvotes: 0