javascript 如何在javascript中检查字符串是否包含子字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/47587466/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 07:29:15  来源:igfitidea点击:

How to check string contains substring in javascript

javascriptangularjs

提问by Rashmi Chawdhari

I have strings as below, I am search text order in all variables but I want to return true for string2and string3but for string1should be false.

我有如下字符串,我在所有变量中搜索文本顺序,但我想返回 true forstring2并且string3forstring1应该是 false。

var string1 = "orders"
var string2 = "orders/pack"
var string3 = "orders/123"
var str = "order"
console.log(string1.includes(str)); //false 
console.log(string2.includes(str)); //true
console.log(string3.includes(str)); //true

回答by Anton

You can make check like this:

您可以像这样进行检查:

console.log( string1.includes(str) && (string1 != str) );

回答by Aakash

var string1 = "orders"
var string2 = "orders/pack"
var string3 = "orders/123"
var str = "/"
console.log(string1.includes(str)); //false 
console.log(string2.includes(str)); //true
console.log(string3.includes(str)); //true

Why are you not using '/' instead of 'orders'? Any boundations which we dont know?

为什么不使用“/”而不是“订单”?有哪些我们不知道的界限?

回答by Vinoth

you can try like this it will work

你可以像这样尝试它会起作用

        var string1 = "orders"
        var string2 = "orders/pack"
        var string3 = "orders/123"
        var str = "orders/"

        var result = string1.includes(str);
        console.log(result)
        var result = string2.includes(str);
        console.log(result)
        var result = string3.includes(str);
        console.log(result)