HomeBrew
HomeBrew

Reputation: 869

Compare contents of variable against contents of another variable - jQuery

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

Answers (3)

erimerturk
erimerturk

Reputation: 4288

if (place.indexOf(name ) >=0) {
  // …
}

Upvotes: 2

Jayendra
Jayendra

Reputation: 52789

You can use simple javascript -

if (place.indexOf(name) != -1) {
   alert('do something');
}

Upvotes: 0

Guillaume Cisco
Guillaume Cisco

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

Related Questions