So I am trying to make an array of every word in a text and the array should be like [word, startIndex, endIndex]
. I am going to use this to replace words after, after checking the word-type and find a synonym for it to replace it with. But the problem I am facing is splitting each word and storing the start and end index. text.match(/\b(\w+)\b/g)
works, but I do not get the start and end index that I need. I also tried making some function to parse the text, but it ended up overcomplicated and not really working like it should.
So i wondered if anybody in the javascript community here has a better solution or know how to make an easy function for it.
This is what I would like to happen.
Input:
Norway, officially the Kingdom of Norway, is a sovereign state and unitary monarchy whose territory comprises the western portion of the Scandinavian Peninsula
Output:
['Norway', 0, 6], ['officially', 8, 18]
And the same for all words