jQuery有/包含单词

jQuery has/ contains words

本文关键字:包含单 jQuery      更新时间:2023-09-26

我想实现这一点,如果" 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;
}

如果有人能帮助我,我该如何完成这项任务?

使用(原生JavaScript)函数String.indexOf():

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

.indexOf()返回字符串的索引。如果没有找到该字符串,则返回-1

一个更简洁的解决方案是直接返回条件的值:

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

你可以试试这个

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

或注意区分大小写

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

使用正则表达式来测试大小写

if(/clientpsseabq/.test(Var_words)){
    //given string exists
} else {
    //given string does not exists
}
 if(Var_words.indexOf("clientpsseabq") >= 0))
 {
 }