I have a JS string of this form:
const pageContent = "He stood up and asked the teacher, "Can you elaborate the last point please? ".
I want to map the words to a page and represent it in a view such that the words are clickable. Is there a way to be extract portions of the string using the word index such that any punctuation marks occuring before and after any words are not clickable but the words are? For instance the word Can is clickable but not "Can?
PS: I cannot use the split function to convert the string into an array and map it as this can lead to failure in some edge cases and alters the text flow when converted back into a string. I have the indexes of all the words in a separate array with the starting and end index of each word in the string and want to use these.
Any help is highly appreciated
Simple use two for loops and check if the word matches any of the words in the excluded words array
const pageContent = 'He stood up and asked the teacher, "Can you elaborate the last point please?", Can you elaborate the last point please? '
const indexes = [
[35, 39],
[9, 11]
]
const excluded_words = ['"Can']
for (let i = 0, len = indexes.length; i < len; i++) {
const word = pageContent.substring(indexes[i][0], indexes[i][1]);
for (let i = 0, len = excluded_words.length; i < len; i++) {
if (word.indexOf(excluded_words[i]) > -1) {
console.log(word, " is excluded");
break;
}
if (i === len - 1) {
console.log(word, " is included")
}
}
}