JS判断字符串中是否存在某个字符

JS 判断字符串中是否存在某个字符

可以用 String 中的 indexOf()

1
2
3
4
5
6
7
8
let str = 'abcd'

if (str.indexOf('a') == -1) {
//等于-1表示这个字符串中没有a这个字符
console.log('不含这个字符')
} else {
console.log('含有这个字符')
}