Replacing newline character in javascript

后端 未结 4 1709
生来不讨喜
生来不讨喜 2021-02-07 02:46

I am trying to replaces instances of \\r or \\n characters in my json object with
for display on a website.

I tried:<

4条回答
  •  时光说笑
    2021-02-07 03:24

    Try this:

    myString = myString.replace(/[\r\n]/g, "
    ");

    Update: As told by Pointy on the comment below, this would replace a squence of \r\n with two
    , the correct regex should be:

    myString = myString.replace(/\r?\n/g, "
    ");

提交回复
热议问题