admin管理员组

文章数量:1404573

I have strings as below, I am search text order in all variables but I want to return true for string2 and string3 but for string1 should be 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

I have strings as below, I am search text order in all variables but I want to return true for string2 and string3 but for string1 should be 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

Share Improve this question edited Dec 1, 2017 at 6:06 Satpal 133k13 gold badges167 silver badges170 bronze badges asked Dec 1, 2017 at 6:00 Rashmi ChawdhariRashmi Chawdhari 211 gold badge1 silver badge3 bronze badges 11
  • From what I am seeing, all three strings contain the substring order. Is there a typo, or do you want to include some more explanation? – Tim Biegeleisen Commented Dec 1, 2017 at 6:05
  • I have created a snippet for you and the code works. see if you can reproduce the issue. – Satpal Commented Dec 1, 2017 at 6:06
  • str is a substring of string1 – Mayank Commented Dec 1, 2017 at 6:06
  • 1 This code returns 3 true for me. – Vincent Decaux Commented Dec 1, 2017 at 6:07
  • 1 from your code snipp logic should be contains the word and should contain (/) in word – programtreasures Commented Dec 1, 2017 at 6:18
 |  Show 6 more ments

3 Answers 3

Reset to default 1

You can make check like this:

console.log( string1.includes(str) && (string1 != str) );
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?

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)

本文标签: angularjsHow to check string contains substring in javascriptStack Overflow