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
3 Answers
Reset to default 1You 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
版权声明:本文标题:angularjs - How to check string contains substring in javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744825726a2627048.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论