Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I've got this:

var quoted_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.

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?

like image 698
user2484836 Avatar asked Jun 14 '13 05:06

user2484836


People also ask

How do you turn a string into an array in JavaScript?

The string in JavaScript can be converted into a character array by using the split() and Array. from() functions.

How do you display an array of elements in a new line?

To add a line break in array values for every occurrence of ~, first split the array. After splitting, add a line break i.e. <br> for each occurrence of ~.

How do you join a new line in JavaScript?

The newline character is \n in JavaScript and many other languages. All you need to do is add \n character whenever you require a line break to add a new line to a string.

How do you print each element of a list on a new line in JavaScript?

Use the join() method to combine the array into one string and separate each entry by by <br> to print array on separate lines in JavaScript.


Video Answer


2 Answers

Use split()

Fore example

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

will print out

["abc", "def"]  
like image 73
Akusete Avatar answered Oct 05 '22 23:10

Akusete


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

var str="How\nare\nyou\ndoing\ntoday?"; var n = str.split("\n"); for(var x in n){        n[x]= '>'+n[x];      alert(n[x]); } 
like image 30
codeVerine Avatar answered Oct 06 '22 00:10

codeVerine