javascript - jQuery 有/包含单词

标签 javascript jquery

<分区>

我想实现这一点,如果“clientpsseabq”字符串包含在变量 Var_words 中,则等于 true,否则 错误。我只是不知道我需要使用什么方法或功能?

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);

关于javascript - jQuery 有/包含单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16143151/

相关文章:

javascript - 使用单个数组 Angular c​​omponent.html 显示矩阵

javascript - 使用javascript限制输入标签中的数值

javascript - 仅在一定范围内更改滚动上的 div 类

jQuery 延迟对象回调参数和动态构建?

javascript - 使用带有递增 id 的 jquery clone 添加更多添加更少

javascript - jCarousel 动画

javascript - 有没有办法修改 Canvas 中的像素而不必复制整个缓冲区?

javascript - 如何跟踪具有一个循环列表项提供不同参数的有序列表中的顺序

javascript - 更改克隆字段中的 aria-control 属性值

javascript - 在数据表上显示列的总和(总计)并导出到 Excel