如果“clientpsseabq
”字符串包含在变量Var_words
中,则等于true
,否则false
,我想实现这一点。我只是不知道我需要使用什么方法或功能?
var Var_words = "https://www.go.me/outputsearchs/clientpsseabq"
if ( Var_words contains string "`clientpsseabq`"){
return true;
}else{
return false;
}
如果有人可以帮助我,我该如何完成这项任务?
答案 0 :(得分:3)
你可以试试这个
if (Var_words.indexOf("clientpsseabq") >= 0)
或关注区分大小写
if (Var_words.toLowerCase().indexOf("clientpsseabq") >= 0)
{
// your code
}
答案 1 :(得分:3)
使用(本机JavaScript)函数String.indexOf()
:
if(Var_words.indexOf('clientpsseabq') !== -1) {
return true;
} else {
return false;
}
.indexOf()
返回字符串的索引。如果找不到该字符串,则返回-1
。
更小,更清晰的解决方案是直接返回条件的值:
return (Var_words.indexOf('clientpsseabq') !== -1);
答案 2 :(得分:1)
if(Var_words.indexOf("clientpsseabq") >= 0))
{
}
答案 3 :(得分:1)
使用regular expression来测试案例
if(/clientpsseabq/.test(Var_words)){
//given string exists
} else {
//given string does not exists
}