Javascript 在javascript中删除一行文本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2528076/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 00:45:46  来源:igfitidea点击:

Delete a line of text in javascript

javascripttext

提问by Shard

In javascript, If i have a text block like so

在 javascript 中,如果我有这样的文本块

Line 1
Line 2
Line 3

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

我需要做什么来让我们说删除第一行并将其变成:

Line 2
Line 3

回答by Dan Story

The cleanest way of doing this is to use the split and join functions, which will let you manipulate the text block as an array of lines, like so:

最简洁的方法是使用 split 和 join 函数,它可以让您将文本块作为行数组进行操作,如下所示:

// break the textblock into an array of lines
var lines = textblock.split('\n');
// remove one line, starting at the first position
lines.splice(0,1);
// join the array back into a single string
var newtext = lines.join('\n');

回答by Kozuch

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++:

这将从多行字符串变量中删除第一行 - 在 Chrome 版本 23 中对从文件 (HTML5) 读取的变量进行测试,行尾/换行符在 Notepad++ 中显示为 CRLF(回车 + 换行):

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!

希望有帮助!

回答by LesterDove

In a nutshell: Look for the first line return (\n) and use the JavaScript replacefunction to remove everything up to it (and including it.)

简而言之:查找第一行 return (\n) 并使用 JavaScriptreplace函数删除它之前的所有内容(包括它。)

Here is a RegEx that does it (surprisingly tricky, at least for me...)

这是一个可以做到这一点的 RegEx(出人意料地棘手,至少对我来说......)

<script type = "text/javascript">
var temp = new String('Line1\nLine2\nLine3\n');
temp = temp.replace(/[\w\W]+?\n+?/,"");
alert (temp);
</script>

回答by Eli Grey

var firstLineRemovedString = aString.replace(/.*/, "").substr(1);

回答by Ellis Percival

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.

您可以通过在搜索中包含换行符来使用正则表达式执行此操作。如果要删除的行不在开头,则必须使用多行 (m) 标志。

> 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

回答by Matt Walterspieler

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 Xis the number of line you want to delete +1 (?:.*?\n){X}(?:.*?\n)

我使用这个正则表达式 where Xis 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));