I have a task. I need to highlight specific words in income HTML. To find the exact word i need to use its coordinates. Coordinates are calculating according to word`s first and last letter position in the string.
Example
I have such constant
const exmpleString: string = `<h1>Name</h1> <p>Some some, text some.</p>`;
I need to highlight word text
.
Its coordinates is [28,31] - because first t
is on position 28
in the string and last t
is on position 31
.
I found out how to make it with regex.
const testString: string = 'some some some text some some text';
const keyWord: string= 'text';
const regexRule: any = new RegExp(keyWord, 'g');
const result: string = testString.replace(regexRule, `<span class="some">${keyWord}</span>`);
console.log(result);
Works great but i need other solution.