title: Replace, all at once, rest of a group of words with span
body: <p>In the following example, first I replace successfully the 'my own' with its own span <code>'&lt;span id=&quot;sMyOwn&quot;&gt;my own&lt;/span&gt;'</code>. Next, I want to replace, all at once, the rest of the words (that is, 'This', 'is' and 'example') with a simple span. For this, I use the function fReplace, in order to do it conditionally according to @Abhilash comment that follows, but it fails. The second and the third solutions I applied are based on a simple loop solution, but they fail too. Could someone please help me. The last line is just to present the results.</p>
  <pre><code>&lt;div id='MyOwnExample'&gt;This is my own example&lt;/div&gt;
  &lt;div id='AA'&gt;&lt;/div&gt;
  
  &lt;script&gt;
  // 1st part: It works
      v1 = document.getElementById('MyOwnExample').innerHTML;
      const regex = new RegExp('my own', 'gi')
      v1 = v1.replace(regex,`&lt;span id = 'sMyOwn'&gt;my own&lt;/span&gt;`)
      document.getElementById('MyOwnExample').innerHTML = v1;
  
  // 2nd part: First solution that fails
      function fReplace(word, index) {
          const regex = new RegExp(word, 'gi')
          if (span.className != 'sMyOwn') {
              v1 = v1.replace(regex,`&lt;span&gt;${word}&lt;/span&gt; `)
          }
      }
      v1.forEach(fReplace);
      document.getElementById('MyOwnExample').innerHTML = v1;
  
  // 2nd part: Second solution that fails too 
  const regex2 = new RegExp(word, 'gi')
  for (word of v1) {
      if (span.className != 'sMyOwn') {
          v1 = v1.replace(regex2,`&lt;span&gt;${word}&lt;/span&gt; `)
      }
  }
  document.getElementById('MyOwnExample').innerHTML = v1;
  
  // 2nd part: Third solution that fails too 
  for (word of v1) {
      if (typeof(span) == 'undefined') {
          v1 = v1.replace('${word} ', `&lt;span&gt;${word}&lt;/span&gt; `)
      }
  }
  document.getElementById('MyOwnExample').innerHTML = v1;
  
  
  // 4th part: Results
      document.getElementById('AA').innerText = document.getElementById('MyOwnExample').innerHTML;
  
  &lt;/script&gt;
  </code></pre>