Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy does the string disappear when I try to test it against a regex?
    text
    copied!<p>What I'm trying to do is find all matches in a string, then wrap them in a styled span tag. The solution I've come up with uses recursion, here's the code:</p> <pre><code>function foo(node, pattern) { // alert(node.nodeValue); // alert(node.nodeValue + '\n' + pattern.test(node.nodeValue)); if (pattern.test(node.nodeValue)) { // alert(node.nodeValue); var span = document.createElement('span'); var text = document.createTextNode(RegExp.rightContext); span.className = 'someClass'; span.innerHTML = RegExp.$1; node.nodeValue = RegExp.leftContext; node.parentNode.insertBefore(span, node.nextSibling); node.parentNode.insertBefore(text, span.nextSibling); foo(text, pattern); } return; } </code></pre> <p>See it in action at this fiddle: <a href="http://jsfiddle.net/Umcaf/" rel="nofollow">http://jsfiddle.net/Umcaf/</a></p> <p>The issue I'm having is that it will not mark out all instances of the word 'test'. If you play around with the alert statements, you will notice some odd behavior (it's odd to me anyway). If you uncomment the very first alert, you will see all the correct strings I wish to test, but even though the very last string has the word 'test' in it, <code>pattern.test(node.nodeValue)</code> does not yield 'true'. This is very perplexing.</p> <p>Naturally I came up with the second alert statement. When the second alert statement is used, the it alerts incorrect strings (and by that I mean it doesn't alert the very first full one and adds a last one that contains only white space) and the first occurrence of 'test' isn't marked, but the last two are!</p> <p>I'm fairly new to regular expressions but I thought I understood them well enough until now. Can anyone explain what is happening here?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload