在这里,我用正则表达式匹配一个字符串,然后用两种不同的方式切片:
const string = 'hi hi'
const word = 'hi'
const regex = new RegExp(word, 'gi');
while (match = regex.exec(string)) {
console.log(match.index, regex.lastIndex)
console.log('slice with regex.lastIndex:', string.slice(match.index, regex.lastIndex))
console.log('slice with string\'s length:', string.slice(match.index, word.length))
}
字符串
结果:
0 2
slice with regex.lastIndex: hi
slice with string's length: hi
3 5
slice with regex.lastIndex: hi
slice with string's length:
型
为什么第二次切片不起作用?
1条答案
按热度按时间7rtdyuoh1#
我想加
match.index
字符串