Delete a line of text in javascript

前端 未结 7 1594
甜味超标
甜味超标 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 17:58

    I went a bit further to let you be able to select the number of lines from the beginning you want to delete:

    I use this regular expression where X is the number of line you want to delete +1 (?:.*?\n){X}(?:.*?\n)

    const lines = `Line1
    Line2
    Line3
    Line4`;
    const deleteLines = (string, n = 1)=>{
      return string.replace(new RegExp(`(?:.*?\n){${n-1}}(?:.*?\n)`), '');
    };
    console.log(deleteLines(lines, 2));

提交回复
热议问题