I have this code from this post where it change the value of a string except html tags.
const regEx = new RegExp("(" + searchWord + ")(?!([^<]+)?>)";
//new RegExp(`/^(${searchString})(?!([^<]+)?>)/`, 'gi');
setContent(content_?.replace(regEx, `<mark>${searchString}</mark>`)); //using React
but my problem is it changes all the string even at the middle of the words. Is it possible to just change the string if it the searchString only starts with it.
Example if I search the string 'age':
Wrong Scenario 1(usage):
const sample1 = '<span>usage</span>'
// WRONG Result '<span>us<mark>age</mark></span>'
usage should not be changed since it doesn't start with 'age'.
Correct Scenario 2 (agencies):
const sample2 = '<span>agencies</span>';
// CORRECT Result '<span><mark>age</mark>ncies</span>'