如何在 JavaScript 中通过换行符从字符串创建数组?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17101972/
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-10-27 07:05:59  来源:igfitidea点击:

How to make an array from a string by newline in JavaScript?

javascriptarraystextnewline

提问by user2484836

I've got this:

我有这个:

var quoted_text = window.getSelection;

varquoted_text = window.getSelection;

For example:

例如:

Accepting the Terms of Service

The Stack Exchange Network (the “Network”) is a set of related Internet sites and other applications for questions and answers, owned and operated by Stack Exchange Inc. (“Stack Exchange”), a Delaware corporation. Please read these terms of service (“Agreement”) carefully before using the Network or any services provided on the Network (collectively, “Services”). By using or accessing the Services, you agree to become bound by all the terms and conditions of this Agreement. If you do not agree to all the terms and conditions of this Agreement, do not use the Services. The Services are accessed by You (“Subscriber” or “You”) under the following terms and conditions: 1. Access to the Services

Subject to the terms and conditions of this Agreement, Stack Exchange may offer to provide the Services, as described more fully on the Network, and which are selected by Subscriber, solely for Subscriber's own use, and not for the use or benefit of any third party. Services shall include, but not be limited to, any services Stack Exchange performs for Subscriber, as well as the offering of any Content (as defined below) on the Network. Stack Exchange may change, suspend or discontinue the Services at any time, including the availability of any feature, database, or Content. Stack Exchange may also impose limits on certain features and services or restrict Subscriber's access to parts or all of the Services without notice or liability. Stack Exchange reserves the right, at its discretion, to modify these Terms of Service at any time by posting revised Terms of Service on the Network and by providing notice via e-mail, where possible, or on the Network. Subscriber shall be responsible for reviewing and becoming familiar with any such modifications. Use of the Services by Subscriber following such modification constitutes Subscriber's acceptance of the terms and conditions of this Agreement as modified.

接受服务条款

Stack Exchange 网络(“网络”)是一组相关的互联网站点和其他问答应用程序,由特拉华州一家公司 Stack Exchange Inc.(“Stack Exchange”)拥有和运营。在使用网络或网络上提供的任何服务(统称为“服务”)之前,请仔细阅读这些服务条款(“协议”)。通过使用或访问服务,您同意受本协议的所有条款和条件的约束。如果您不同意本协议的所有条款和条件,请勿使用服务。您(“订阅者”或“您”)根据以下条款和条件访问服务: 1. 访问服务

根据本协议的条款和条件,Stack Exchange 可以提供服务,如网络上更详细的描述,并且由订阅者选择,仅供订阅者自己使用,而不是为了任何第三方的使用或利益派对。服务应包括但不限于 Stack Exchange 为订户执行的任何服务,以及在网络上提供的任何内容(定义见下文)。Stack Exchange 可随时更改、暂停或中断服务,包括任何功能、数据库或内容的可用性。Stack Exchange 也可能对某些功能和服务施加限制,或限制订阅者对部分或全部服务的访问,恕不另行通知或承担责任。Stack Exchange 保留自行决定的权利,随时修改这些服务条款,方法是在网络上发布修订后的服务条款,并在可能的情况下通过电子邮件或在网络上提供通知。订户应负责并熟悉任何此类修改。订户在此类修改后使用服务即表示订户接受经修改的本协议的条款和条件。

How can i make in array from that text by newlines?

如何通过换行符从该文本中创建数组?

I need to paste in the begining of each line simbols "> ", how to do that?

我需要在每一行的开头粘贴符号“>”,怎么做?

回答by Akusete

Use split()

使用split()

Fore example

举例

str = "abc\ndef";
console.log(str.split("\n"));

will print out

会打印出来

["abc", "def"] 

回答by codeVerine

Use javascript .split()function to create an array with elements splitted by '\n' and then manually iterate through that array and add '<' for each item. The following code may help :

使用 javascript.split()函数创建一个由 '\n' 分割的元素的数组,然后手动遍历该数组并为每个项目添加 '<'。以下代码可能会有所帮助:

var str="How\nare\nyou\ndoing\ntoday?";
var n = str.split("\n");
for(var x in n){   
    n[x]= '>'+n[x]; 
    alert(n[x]);
}