Delete a line of text in javascript

前端 未结 7 1559
甜味超标
甜味超标 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:15

    This removes the first line from a multi-line string variable - tested in Chrome version 23 on a variable which was read from file (HTML5) with line endings/breaks that showed as CRLF (carriage return + line feed) in Notepad++:

    var lines = `first
    second
    third`;
    
    // cut the first line:
    console.log( lines.substring(lines.indexOf("\n") + 1) );
    
    // cut the last line:
    console.log( lines.substring(lines.lastIndexOf("\n") + 1, -1 ) )

    Hope that helps!

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