function fn(str, target) { let res = new RegExp(target, 'g').exec(str) return res ? res.index : -1 } console.log('打印', fn('hello word', 'l')) // 打印 2