Check if text is in a string
I want to check is some text is in a string for instance i have a string
str = "car, bycicle, bus"
and I have another string
str2 = "car"
I want to check if str2 is in str.
I am a newbie in javascript so please bear with me :)
Regards
if(str.indexOf(str2) >= 0) {
...
}
Or if you want to go the regex route:
if(new RegExp(str2).test(str)) {
...
}
However you may face issues with escaping (metacharacters) in the latter, so the first route is easier.
str.lastIndexOf(str2) > 0; this should work. untested though.
请使用这个:
var s = "foo";
alert(s.indexOf("oo") > -1);
链接地址: http://www.djcxy.com/p/12858.html
上一篇: 如何检查一个不是
下一篇: 检查文本是否在字符串中