JavaScript/jQuery - 如何检查字符串是否包含特定单词
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5388429/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JavaScript/jQuery - How to check if a string contain specific words
提问by Charles Yeung
$a = 'how are you';
if (strpos($a,'are') !== false) {
echo 'true';
}
In PHP, we can use the code above to check if a string contain specific words, but how can I do the same function in JavaScript/jQuery?
在 PHP 中,我们可以使用上面的代码来检查一个字符串是否包含特定的单词,但是如何在 JavaScript/jQuery 中执行相同的功能呢?
采纳答案by Stephen Chung
If you are looking for exact wordsand don't want it to match things like "nightmare" (which is probably what you need), you can use a regex:
如果您正在寻找确切的单词并且不希望它与“噩梦”之类的东西相匹配(这可能是您需要的),您可以使用正则表达式:
/\bare\b/gi
\b = word boundary
g = global
i = case insensitive (if needed)
If you just want to find the characters "are", then use indexOf
.
如果您只想查找字符“are”,请使用indexOf
.
If you want to match arbitrary words, you have to programatically construct a RegExp (regular expression) object itself based on the word string and use test
.
如果要匹配任意单词,则必须根据单词 string 以编程方式构建 RegExp(正则表达式)对象本身并使用test
.
回答by naiquevin
you can use indexOf for this
您可以为此使用 indexOf
var a = 'how are you';
if (a.indexOf('are') > -1) {
return true;
} else {
return false;
}
Edit: This is an old answer that keeps getting up votes every once in a while so I thought I should clarify that in the above code, the if
clause is not required at all because the expression itself is a boolean. Here is a better version of it which you should use,
编辑:这是一个旧的答案,每隔一段时间就会获得投票,所以我想我应该澄清一下,在上面的代码中,if
根本不需要该子句,因为表达式本身是一个布尔值。这是您应该使用的更好版本,
var a = 'how are you';
return a.indexOf('are') > -1;
Update in ECMAScript2016:
ECMAScript2016 中的更新:
var a = 'how are you';
return a.includes('are'); //true
回答by vsync
indexOf
should not be used for this.
indexOf
不应该用于此。
CORRECTfunction:
正确的功能:
function wordInString(s, word){
return new RegExp( '\b' + word + '\b', 'i').test(s);
}
wordInString('did you, or did you not, get why?', 'you') // true
This will find a word, real word, not just if the letters of that word are somewhere in the string.
这将找到一个单词,真正的单词,而不仅仅是该单词的字母是否在字符串中的某个位置。
回答by evbailey
You're looking for the indexOf function:
您正在寻找 indexOf 函数:
if (str.indexOf("are") >= 0){//Do stuff}
回答by Neelansh Verma
You might wanna use includemethod in JS.
您可能想在 JS 中使用include方法。
var sentence = "This is my line";
console.log(sentence.includes("my"));
//returns true if substring is present.
PS: includes is case sensitive.
PS:包含区分大小写。
回答by pgksunilkumar
var str1 = "STACKOVERFLOW";
var str2 = "OVER";
if(str1.indexOf(str2) != -1){
console.log(str2 + " found");
}
回答by Akshay kumar
An easy way to do it to use Regex match() method :-
使用 Regex match() 方法的简单方法:-
For Example
例如
var str ="Hi, Its stacks over flow and stackoverflow 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');
}
Check the fiddle
检查小提琴
NOTE: For adding case sensitiveness update the regex with /(^|\W)stack($|\W)/i
注意:要添加区分大小写,请使用以下内容更新正则表达式 /(^|\W)stack($|\W)/i
Thanks
谢谢
回答by Jahid
This will
这会
/\bword\b/.test("Thisword is not valid");
return false
, when this one
返回false
,当这个
/\bword\b/.test("This word is valid");
will return true
.
会回来true
。
回答by Zeni
In javascript the includes() method can be used to determines whether a string contains particular word (or characters at specified position). Its case sensitive.
在 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