2011-11-03 79 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'); 
}