2011-11-03 85 views
0

我正在创建页面搜索函数,我需要比较一个变量的内容以查看它是否存在于if语句中的另一个变量中。将变量的内容与另一个变量的内容进行比较 - jQuery

要在这里简单的是一个expample:

var name = 'james'; 
var place = 'james lives in europe'; 

if (place:contains(james)) { 
    ....... do something 
} 

任何人都知道如何使用jQuery做到这一点。欢呼声

回答

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

是的,你可以使用正则表达式:

var str="james lives in europe"; 
var reg1=new RegExp("[james]","g"); 
if (str.match(reg1)) { 
    document.write("'[james]' is in str"); 
} 
0

您可以使用简单的JavaScript -

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