title: How can I use a Regular Expression to replace everything except specific words in a string with Javascript
body: <p>Imagine you have a string like this: "This is a sentence with words."</p>
  
  <p>I have an array of words like <code>$wordList = ["sentence", "words"];</code></p>
  
  <p>I want to highlight words that <em>aren't</em> on the list. Which means I need to find and replace everything else and I can't seem to crack how to do that (if it's possible) with RegEx.</p>
  
  <p>If I want to match the words I can do something like:</p>
  
  <p><code>text = text.replace(/(sentence|words)\b/g, '&lt;mark&gt;$&amp;&lt;/mark&gt;');</code></p>
  
  <p>(which will wrap the matching words in "mark" tags and, assuming I have some css for <code>&lt;mark&gt;</code>, highlight them) which works perfectly. But I need the opposite! I need it to basically select the entire string and then exclude the words listed. I've tried <code>/^((?!sentence|words)*)*$/gm</code> but this gives me a strange infinity issue because I think it's too open ended.</p>
  
  <p>Taking that original sentence, what I would hope to end up with is <code>"&lt;mark&gt; This is a &lt;/mark&gt; sentence &lt;mark&gt; with some &lt;/mark&gt; words."</code></p>
  
  <p>Basically wrapping (via replace) everything <em>except</em> the words listed.</p>
  
  <p>The closest I can seem to get is something like <code>/^(?!sentence|words).*\b/igm</code> which will successfully do it if a line <em>starts</em> with one of the words (ignoring that entire line).</p>
  
  <p>So to summarize: 1) Take a string 2) take a list of words 3) replace everything in the string <em>except</em> the list of words.</p>
  
  <p>Possible? (jQuery is loaded for something else already, so raw JS or jQuery are both acceptable).</p>