2013-04-22 27 views
3

我想實現這一點,如果「clientpsseabq」字符串包含在變量Var_words然後等於true,否則false。我只是不知道我需要使用什麼方法或功能?jQuery有/包含文字

var Var_words = "https://www.go.me/outputsearchs/clientpsseabq" 

if (Var_words contains string "`clientpsseabq`"){ 
    return true; 
}else{ 
    return false; 
} 

如果有人能幫助我,我該如何完成此任務?

回答

3

使用(本地JavaScript)功能String.indexOf()的:

if(Var_words.indexOf('clientpsseabq') !== -1) { 
    return true; 
} else { 
    return false; 
} 

.indexOf()返回的索引串。如果未找到該字符串,則返回-1

更小,更清潔的解決方案是簡單地返回直接有條件的價值:

return (Var_words.indexOf('clientpsseabq') !== -1); 
+0

感謝您的回答, 'indexof'應該是'indexOf',否則它不起作用。 – olo 2013-04-22 20:33:54

+1

所以應該,謝謝。我一定是匆匆打錯了 – Bojangles 2013-04-22 21:12:49

3

你可以試試這個

if (Var_words.indexOf("clientpsseabq") >= 0) 

或小心區分大小寫

if (Var_words.toLowerCase().indexOf("clientpsseabq") >= 0) 
{ 
    // your code 
} 
1
if(Var_words.indexOf("clientpsseabq") >= 0)) 
{ 

} 
1

使用regular expression來測試的情況下

if(/clientpsseabq/.test(Var_words)){ 
    //given string exists 
} else { 
    //given string does not exists 
}