I have a long text and an array that contains a few objects. every object has a start key and an end key. What I am trying to do is to highlight the text, only on indexes I get from the array.
I wrote this code:
function HighlightRange(text: any, start: any, end: any, color: any) {
return (
text.substring(0, start) +
`<span style="background-color: ${color}">` +
text.substring(start, end) +
`</span>` +
text.substring(end)
);
}
It works perfectly if I have only one object in the array. The thing is that when I use it, it changes the text and now it has a bigger length and the highlight now needs to get the new length in order to highlight the exact range.
I tried that:
value?.input_spans.map((value: any, key: any) => {
newString = HighlightRange(
originalTextArea,
value.start,
value.end,
"lightgreen"
);
});
Is it possible to iterate over all objects and highlight all text from start to end without changing the text length?