Bug in JavaScript V8 regex engine when matching beginning-of-line? <p>I have a pretty nifty tool, <a href="https://github.com/ddopson/underscore-cli" rel="nofollow">underscore-cli</a>, that's getting the strangest behavior when printing out the help / usage information.</p> <p>In the usage() function, I do this to indent blocks of text (eg, the options):</p> <pre><code>str.replace(/^/, " "); </code></pre> <p>This regex, in addition to being pretty obvious, comes straight out of TJ Hollowaychuk's <a href="https://github.com/visionmedia/commander.js" rel="nofollow">commander.js</a> code. The regex is correct.</p> <p>Yet, I get bizzare spaces inserted into the middle of my usage text. like this:</p> <pre> Commands: ... values Retrieve all the values of an object's properties. extend &ltobject> Override properties in the input data. defaults &ltobject> Fill in missing properties in the input data. any &ltexp> Return 'true' if any of the values in the input make the expression true. Expression args: (value, key, list) all &ltexp> Return 'true' if all values in the input make the expression true. Expression args: (value, key, list) isObject Return 'true' if the input data is an object with named properties isArray Return 'true' if the input data is an array isString Return 'true' if the input data is a string ... </pre> <p>99% chance, this HAS to be a bug in V8.</p> <p>Anyone know why this happens, or what the easiest work-around would be? </p> <p>Yup, turns out this <strong>IS</strong> a V8 bug, 1748 to be exact. Here's <a href="https://github.com/ddopson/underscore-cli/commit/bad0802b1fdc65de8623a7950a831bf5828bf283" rel="nofollow">the workaround I used in the tool</a>:</p> <pre><code>str.replace(/(^|\n), "$1 "); </code></pre>