I have following JS -
var pattern = new RegExp("(\\b" + value + "\\b)", 'gi')
if (pattern.test(text)) {
text = text.replace(pattern, "<span class='class1'>$1</span>");
}
(e.g. value = 'text', text = 'Simple text <span class='class1'>simple text</span> text.')
with this JS I wrapp all 'text' words with span tag. But in this case I will have second 'text' word double wrapped with span tag. I need to replace all words that are equal Value variable but if this word is already in the span with Class1 I need to skip this word.
EXAMPLE: Initial text was: "When using alternative, the order is important since the matching algorithm will attempt to match the leftmost alternative first.". I need to wrap all 'using alternatives' and 'alternatives' words. First we replace 'using alternatives', the result will be:
"When <span class="class1">using alternative</span>, the order is important since the matching algorithm will attempt to match the leftmost alternative first."
Then we replace "alternative":
"When <span class="class1">using <span class="class1">alternative</span></span>, the order is important since the matching algorithm will attempt to match the leftmost <span class="class1">alternative</span> first.".
So I have first "alternative" wrapped with 2 span tags. And I do not need that second tag for "alternative", it is enough to have 'using alternative'.