Javascript 如何检查一个字符串是否不包含另一个字符串的文本?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35355920/
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
How can I check that a string does not include the text of another string?
提问by
I have this javascript code:
我有这个 javascript 代码:
if (fromState.name == "home.subjects.subject.exams.exam.tests.test" &&
toState.name == "home.subjects.subject.exams.exam.tests") {
tes.test.current = false;
tes.test = null;
}
I understand that I can do a simple match here:
我知道我可以在这里做一个简单的匹配:
toState.name == "home.subjects.subject.exams.exam.tests"
To check the toState name.
检查 toState 名称。
But how could I check the toState.name does notinclude the string:
但是,我怎么能检查toState.name并没有包含字符串:
"home.subjects.subject.exams.exam.tests" ?
for example the toStateName could be:
例如 toStateName 可以是:
"home" or "home.access" or "home.city"
采纳答案by Ananth
回答by Allison
You could use includes
and negate it.
你可以使用includes
和否定它。
!str1.includes(str2)
!str1.includes(str2)
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/includes
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/includes
回答by briflan26
var include_flag = toState.name.includes("home.subjects.subject.exams.exam.tests");
return !include_flag;
Using the JS includes method would probably be your best bet. I get I'm a little late in answering this question, but I was just googling this myself and came up with this answer after some fiddling with the code. This code will return true if toState.name
does NOTinclude the string given.
使用 JS 包含方法可能是你最好的选择。我知道我回答这个问题有点晚了,但我自己只是在谷歌上搜索这个,并在对代码进行了一些摆弄之后想出了这个答案。如果此代码将返回真toState.name
不不包括给定的字符串。
Hope this helps anyone searching the same question I had!
希望这可以帮助任何搜索我遇到的相同问题的人!
回答by gurvinder372
But how could I check the toState.name does not include the string:
但是我怎么能检查 toState.name 不包含字符串:
var strArr = "home.subjects.subject.exams.exam.tests.test".split(".");
var name = "home.subjects.subject.exams.exam.tests";
var contains = false;
strArr.reduce( function(prev, current){
if ( name.indexOf( current ) != -1 )
{
contains = true;
}
return prev + "." + current;
} );
if (contains)
{
alert( "yes it contains" );
}
else
{
alert( "Nope" );
}
回答by Daithi66
The original question contained a "?" ternary operator, so here is how to do it using a ternary operator.
原始问题包含一个“?” 三元运算符,所以这里是如何使用三元运算符来做到这一点。
Suppose you're writing a poker game and have a rainbow flop. Now suppose you want the missing suit from the flop.
假设你正在编写一个扑克游戏并且有一个彩虹翻牌。现在假设您想要翻牌圈丢失的花色。
let flop = "3h Js 9c";
let missingSuit = !flop.includes("c") ? "c" :
!flop.includes("d") ? "d" :
!flop.includes("h") ? "h" : "s";
// missingSuit equals "d"
回答by Touseef Murtaza
For me jQuery Version is 1.10.2
对我来说 jQuery 版本是 1.10.2
To check the substring within the string you can use includesjQuery function like this
要检查字符串中的子字符串,您可以使用包含这样的jQuery 函数
var sub_str = "find"
var main_str = "find me if you can"
result = main_str.includes(sub_str)
if result{
* Stuff todo if result found
}else{
* Stuff todo if result not found
}
result true; // if you have sub string finder method separately and calling it from somewhere else.