Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The error is definitely happening in this line:</p> <pre><code>search.value = search.value.replace(/^"*|"*$/g, "\""); </code></pre> <p>And it is due to the fact that "* matches 0 or more quotes. However, you presumably wouldn't want to just replace it with "+ since that wouldn't do the job you wanted of double-quoting strings with spaces in them.</p> <p>You probably just want to do something like this, in two statements:</p> <pre><code>search.value = search.value.replace(/^"*|"*$/g, '') search.value = '"' + search.value + '"' </code></pre> <p>Part of the key is that there is no 'end of string' character to consume - the regex engine 'just knows' when it is at the end of the string. So after matching a quote at the end of the string, the cursor just moves to the end of the string, and it finds the empty string one more time before falling off the string. Thus, the quote at the end of the string is replaced by a quote, and the 'nothing' at the end of the string is also replaced by a quote.</p> <p>I recommend taking a look at the ECMAScript spec at <a href="http://www.ecma-international.org/publications/files/ECMA-ST/ECMA-262.pdf" rel="nofollow">http://www.ecma-international.org/publications/files/ECMA-ST/ECMA-262.pdf</a> sections 15.5.4.10 and 15.5.4.11 yourself. However, I've also provided an intuitive illustration of how this works at <a href="https://gist.github.com/995501" rel="nofollow">this gist</a>.</p> <p><strong>EDIT</strong>:</p> <p>Since people seem confused as to why this would happen, here's something that might help:</p> <p><a href="http://www.grymoire.com/Unix/Sed.html#uh-6" rel="nofollow">http://www.grymoire.com/Unix/Sed.html#uh-6</a></p> <p>That's from the documentation for sed, but it explains why combining * and /g is a bad idea. The fact that JS doesn't just explode when you do that is a mark in its favor. Note that there are an infinite number of '0 characters' at every position in the string.</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