Javascript replacing new line character

后端 未结 1 784
庸人自扰
庸人自扰 2021-01-21 00:40

This is driving me crazy, I have a string in the format:


twice
imap Test wrote:

> nes
相关标签:
1条回答
  • 2021-01-21 01:33

    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.

    0 讨论(0)
提交回复
热议问题