I have some javascript code that replaces certain words with a span tag to highlight them with a background color in a tinyMCE element:
var oldContent = 'some sample text',
newContent = oldContent.replace(/sample|text/g, '<span style="background-color: #ddd;">$&</span>');
The problem is that the text needs to be updated as the user types so the words get replaced over and over with every keystroke ending up in something like this:
<span style="background-color: #ddd;"><span style="background-color: #ddd;"><span style="background-color: #ddd;">sample</span></span></span>
Is there any way to ignore words already wrapped with the span tags? I tried matching for words with whitespace on the sides instead like / sample | text /
but it matches the whitespace as well not just the word and if one of the words is last in the content in won't match it unless you end it with a space.