js 正則做模糊搜索
網(wǎng)上搜了一下大多數(shù)的教程都是使用 indexof 循環(huán)來查詢,總感覺有點(diǎn)累贅百宇。
下面是使用正則的方法。
/// 需要做查詢的字符串,去空格
const srt = 'hello world'.replace(/\s/g, '')
/// 改造字符串
const regStr = srt.split('').join('|')
const reg = new RegExp(regStr, 'i')
/// 驗(yàn)證
console.log(reg.test('w')) /// true
console.log(reg.test('a')) /// false
console.log(reg.test('aw')) /// true