鉴于这两个字符串:
var first = 'dog,cat,lion';
var second = 'cat';
Run Code Online (Sandbox Code Playgroud)
如何判断第二个var是否是第一个变量中的任何单词(由逗号分隔的单词)?
Dav*_*ing 24
你可以使用Array.indexOf:
if( first.split(',').indexOf(second) > -1 ) {
// found
}
Run Code Online (Sandbox Code Playgroud)
需要IE8支持吗?使用垫片:https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Array/indexOf
Nie*_*sol 10
这可行:
if( first.match(new RegExp("(?:^|,)"+second+"(?:,|$)"))) {
// it's there
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
20268 次 |
| 最近记录: |