Javascript regex matching literal \\n
Stumbled on this little oddity today, can someone explain?
x = 'a \\n b';
x.replace(/\\n/g, '<br>'); // => "a <br> b"
x.replace(RegExp('\\n', 'g'), '<br>'); // => "a \\n b"
x.replace(RegExp('\\n', 'gm'), '<br>'); // => "a <br> b"
I assumed /\\n/g and RegExp('\\n', 'g') would be equivalent, but that
doesn't seem to be the case. In what cases will using one method over the
other give different results?
Why is the multiline flag needed, and only when using a RegExp object?
No comments:
Post a Comment