JavaScript/jQuery - 如何检查字符串是不是包含特定单词
Posted
技术标签:
【中文标题】JavaScript/jQuery - 如何检查字符串是不是包含特定单词【英文标题】:How to check if a string contain specific words?JavaScript/jQuery - 如何检查字符串是否包含特定单词 【发布时间】:2011-07-20 07:03:16 【问题描述】:$a = 'how are you';
if (strpos($a,'are') !== false)
echo 'true';
在 php 中,我们可以使用上面的代码来检查字符串是否包含特定的单词,但是在 javascript/jQuery 中如何执行相同的功能呢?
【问题讨论】:
javascript : string contains ...的可能重复 和JQuery string contains check How to see if string contains substring的可能重复 【参考方案1】:您可以为此使用 indexOf
var a = 'how are you';
if (a.indexOf('are') > -1)
return true;
else
return false;
编辑:这是一个旧的答案,每隔一段时间就会得到投票,所以我想我应该澄清一下,在上面的代码中,if
子句根本不需要,因为表达式本身是一个布尔值。这是您应该使用的更好版本,
var a = 'how are you';
return a.indexOf('are') > -1;
ECMAScript2016 更新:
var a = 'how are you';
return a.includes('are'); //true
【讨论】:
非常糟糕的答案。这将失败:'你好' @vsync 为什么你说“不好的答案”?因为他删除了 IF 语句?那么,您对预编辑有何看法? (编辑前) @Shafizadeh - 不是因为if
。您是否甚至阅读了我的答案以了解为什么在这种情况下使用 indexof
是不好的?为什么你甚至认为这是因为如果if
无论如何?【参考方案2】:
indexOf
/includes
不应用于查找整个单词:
一个词(在西方文化中)是一个相互靠近的符号的图画,每个词之间有一些空间。如果一个词是一个完整的字符组合在一起并且不是部分的一部分,则该词被认为是这样的:
"has a word".indexOf('wor') // 6 ("wor" is not a word in this sentence)
"has a word".includes('ha') // true ("ha" is not a word in this sentence)
检查字符串中是否有单个单词(whole word)
找到一个真正的完整单词,而不仅仅是该单词的字母在字符串中的某个位置。
const wordInString = (s, word) => new RegExp('\\b' + word + '\\b', 'i').test(s);
// tests
[
'', // true
' ', // true
'did', // true
'id', // flase
'yo ', // flase
'you', // true
'you not' // true
].forEach(q => console.log(
wordInString('dID You, or did you NOt, gEt WHy?', q)
))
console.log(
wordInString('did you, or did you not, get why?', 'you') // true
)
检查是否所有单词都在字符串中
var stringHasAll = (s, query) =>
// convert the query to array of "words" & checks EVERY item is contained in the string
query.split(' ').every(q => new RegExp('\\b' + q + '\\b', 'i').test(s));
// tests
[
'', // true
' ', // true
'aa', // true
'aa ', // true
' aa', // true
'd b', // false
'aaa', // false
'a b', // false
'a a a a a ', // false
].forEach(q => console.log(
stringHasAll('aA bB cC dD', q)
))
【讨论】:
我有机会需要 2 检查“你好吗?”、“你疯了吗?”包含字符串“are”(不区分大小写)并且不包含“Awareness is important”。在这种情况下,这个功能非常有用@vsync :) 确保“单词”不包含任何正则表达式元字符,否则匹配将不起作用。单词中的任何正则表达式元字符都必须转义。 +1 用于添加使您正在寻找的单词成为变量的方式。我发现的大多数其他答案都提供了一个修复模式...... 嗯,我读了你的答案,这很好:-)! +1。顺便说一句,我认为使用indexOf
也很好,因为here 和here 是他两个都使用过indexOf
的两个专业人士的两个很好的答案。
@Shafizadeh - 您提到的那些答案不适用于这个特定问题,indexOf
解决方案将失败,无论给出这些答案的人有多“专业”。【参考方案3】:
如果您正在寻找准确的单词,并且不希望它匹配诸如“噩梦”之类的东西(这可能是您需要的),您可以使用正则表达式:
/\bare\b/gi
\b = word boundary
g = global
i = case insensitive (if needed)
如果您只想查找字符“are”,请使用indexOf
。
如果你想匹配任意单词,你必须根据单词字符串以编程方式构造一个RegExp(正则表达式)对象本身并使用test
。
【讨论】:
【参考方案4】:您正在寻找 indexOf 函数:
if (str.indexOf("are") >= 0)//Do stuff
【讨论】:
【参考方案5】:你可能想在 JS 中使用 include 方法。
var sentence = "This is my line";
console.log(sentence.includes("my"));
//returns true if substring is present.
PS:includes 区分大小写。
【讨论】:
【参考方案6】:使用正则表达式 match() 方法的简单方法:-
举例
var str ="Hi, Its stacks over flow and *** Rocks."
// It will check word from beginning to the end of the string
if(str.match(/(^|\W)stack($|\W)/))
alert('Word Match');
else
alert('Word not found');
查看fiddle
注意:要添加区分大小写,请使用 /(^|\W)stack($|\W)/i
更新正则表达式
谢谢
【讨论】:
【参考方案7】:这会
/\bword\b/.test("Thisword is not valid");
返回false
,当这个
/\bword\b/.test("This word is valid");
将返回true
。
【讨论】:
【参考方案8】:var str1 = "***";
var str2 = "OVER";
if(str1.indexOf(str2) != -1)
console.log(str2 + " found");
【讨论】:
【参考方案9】:在 javascript 中,includes() 方法可用于确定字符串是否包含特定单词(或指定位置的字符)。它区分大小写。
var str = "Hello there.";
var check1 = str.includes("there"); //true
var check2 = str.includes("There"); //false, the method is case sensitive
var check3 = str.includes("her"); //true
var check4 = str.includes("o",4); //true, o is at position 4 (start at 0)
var check5 = str.includes("o",6); //false o is not at position 6
【讨论】:
谢谢!这是一个简单的解决方案以上是关于JavaScript/jQuery - 如何检查字符串是不是包含特定单词的主要内容,如果未能解决你的问题,请参考以下文章
Javascript / jQuery:如何检查日期是不是是一个月的最后一天