Delete a line of text in javascript

前端 未结 7 1557
甜味超标
甜味超标 2020-12-13 17:44

In javascript, If i have a text block like so

Line 1
Line 2
Line 3

What would i need to do to lets say delete the first line and turn it in

7条回答
  •  轮回少年
    2020-12-13 18:06

    You can do this with regex by including the newline character in your search. If the line you want to remove is not at the beginning, you must use the multiline (m) flag.

    > var text = "Line 1\nLine 2\nLine 3";
    > console.log(text);
    Line 1
    Line 2
    Line 3
    > text.replace(/^Line 1\n/m, "");
    Line 2
    Line 3
    > text.replace(/^Line 2\n/m, "");
    Line 1
    Line 3
    

提交回复
热议问题