我有一根绳子:
const str = 'a string, a long string'
我想把它分解成单词(这里没有问题),然后跟踪原始字符串中每个单词的索引.
实际结果:
[
{ word: 'a', idx: 0 },
{ word: 'string', idx: 2 },
{ word: 'a', idx: 0 },
{ word: 'long', idx: 12 },
{ word: 'string', idx: 2 }
]
预期结果:
[
{ word: 'a', idx: 0 },
{ word: 'string', idx: 2 },
{ word: 'a', idx: 10 },
{ word: 'long', idx: 12 },
{ word: 'string', idx: 17 }
]
到目前为止的代码:
const str = 'a string, a long string'
const segmenter = new Intl.Segmenter([], { granularity: 'word' })
const getWords = str => {
const segments = segmenter.segment(str)
return [...segments]
.filter(s => s.isWordLike)
.map(s => s.segment)
}
const words = getWords(str)
const result = words.map(word => ({
word,
idx: str.indexOf(word)
}))
console.log(result)