This my script to Highlight a specific word from text string my only problem is when I want to highlight three sequence words it just the 1st word highlighted then 2nd still without highlight then the third one it is highlighted
The * its a truncation and works well
- This an Example to highlight
nsequence words:
var row = {
"Abstract": "This reference is to serve test as a useful reference for testing whether the styling of reference works or not. Adtest all occurrences of 'reference' should be given a color of red tests"
};
//here i need to highlighte "reference" "is" "to" "serve" "test*" (n sequnence words)
var wordsToHighlight = 'reference is to serve test*';
var result = row["Abstract"];
wordsToHighlight.split(" ").forEach(function (word) {
word = word.replace(/\*/g, '\\S*');
result = result.replace(new RegExp('((?:\\s|^)' + word + '(?:\\s|$))', "g"),'<span style="color: red;">$1</span>');
});
document.querySelector("#result").innerHTML = result;
<div id="result"></div>
My object is to highlights all the words in the paragraph
looking for your suggestion.
result.replace(new RegExp('(\\s|^)(' + word + ')(?=\\s|$)', "g"),'$1<span style="color: red;">$2</span>')