Javascript Replacing New Line Character
This is driving me crazy, I have a string in the format:
twice
imap Test wrote:
> nested
>&
twice
imap Test wrote:
> nested
>&
Solution 1:
If your goal is simply to remove all the >s at the beginning of the lines, that's easy to do:
var out = in.replace(new RegExp("^>+", "mg"), "");
or
var out = in.replace(/^>+/mg, "");
The m flag means multi-line so ^ and $ will match the beginning and end of lines, not just the beginning and end of the string. See RegExp Object.
Edit: It's worth mentioning that you should probably favour using the RegExp object over the second form (regex literal). See Are /regex/ Literals always RegExp Objects? and Why RegExp with global flag in Javascript give wrong results?.
In addition there are browser differences in how the global flag is treated on a regex literal that is used more than once. It's best to avoid it and explicitly use a RegExp object.
Post a Comment for "Javascript Replacing New Line Character"