Javascript how to split a new line

I use jquery and I have a text box, so when I submit the button, I will notify each text separately on a new line. How to split from a new line?

var ks = $('#keywords').val().split("\n"); (function($){ $(document).ready(function(){ $('#data').submit(function(e){ e.preventDefault(); alert(ks[0]); $.each(ks, function(k){ alert(k); }); }); }); })(jQuery); 

input example:

 Hello There 

The result I want:

 alert(Hello); and alert(There) 
+75
javascript jquery
Nov 14 '11 at 17:40
source share
11 answers

Try initializing the ks variable inside your submit function.

  (function($){ $(document).ready(function(){ $('#data').submit(function(e){ var ks = $('#keywords').val().split("\n"); e.preventDefault(); alert(ks[0]); $.each(ks, function(k){ alert(k); }); }); }); })(jQuery); 
+70
Nov 14 '11 at 17:44
source share

It should be

 yadayada.val.split(/\n/) 

you pass a literal string to the split command, not the regular expression.

+41
Nov 14 '11 at 17:45
source share

You have to parse newline strings regardless of platform (operating system) This regex split is universal, you can use it.

 var ks = $('#keywords').val().split(/\r?\n/); 

eg.

 "a\nb\r\nc\r\nlala".split(/\r?\n/) // ["a", "b", "c", "lala"] 
+39
Aug 16 '17 at 9:27
source share

Since you are using textarea , you can find \ n or \ r (or \ r \ n) for line breaks. So, the following is suggested:

$('#keywords').val().split(/\r|\n/)

ref: check if string contains line break

+28
Apr 14 '16 at 9:24
source share

Simply

var ks = $('#keywords').val().split(/\r\n|\n|\r/);

will work just fine.

Make sure \r\n is at the beginning of the RegExp line, because it will be tried first.

+6
Jan 11 '18 at 4:40
source share
  • Move var ks = $('#keywords').val().split("\n"); inside event handler
  • Use alert(ks[k]) instead of alert(k)



  (function($){ $(document).ready(function(){ $('#data').submit(function(e){ e.preventDefault(); var ks = $('#keywords').val().split("\n"); alert(ks[0]); $.each(ks, function(k){ alert(ks[k]); }); }); }); })(jQuery); 

Demo

+3
Nov 14 '11 at 17:46
source share

The easiest and safest way to break a line using newlines, regardless of format (CRLF, LFCR or LF), is to remove all carriage return characters and then split them into newlines . "text".replace(/\r/g, "").split(/\n/);

This ensures that if there are continuous newlines (i.e., \r\n\r\n , \n\r\n\r or \n\n ), the result will always be the same.

In your case, the code will look like this:

 (function ($) { $(document).ready(function () { $('#data').submit(function (e) { var ks = $('#keywords').val().replace(/\r/g, "").split(/\n/); e.preventDefault(); alert(ks[0]); $.each(ks, function (k) { alert(k); }); }); }); })(jQuery); 

Here are a few examples showing the importance of this method:

 var examples = ["Foo\r\nBar", "Foo\r\n\r\nBar", "Foo\n\r\n\rBar", "Foo\nBar\nFooBar"]; examples.forEach(function(example) { output('Example "${example}":'); output('Split using "\n": "${example.split("\n")}"'); output('Split using /\r?\n/: "${example.split(/\r?\n/)}"'); output('Split using /\r\n|\n|\r/: "${example.split(/\r\n|\n|\r/)}"'); output('Current method: ${example.replace(/\r/g, "").split("\n")}'); output("________"); }); function output(txt) { console.log(txt.replace(/\n/g, "\\n").replace(/\r/g, "\\r")); } 

+1
Sep 26 '19 at 12:38 on
source share

The problem is that when initializing ks the value parameter is not set.

You should get the value when the user submits the form. So you need to initialize ks inside the callback function

 (function($){ $(document).ready(function(){ $('#data').submit(function(e){ //Here it will fetch the value of #keywords var ks = $('#keywords').val().split("\n"); ... }); }); })(jQuery); 
0
Nov 14 '11 at 17:47
source share

Good'ol javascript:

  var m = "Hello World"; var k = m.split(' '); // I have used space, you can use any thing. for(i=0;i<k.length;i++) alert(k[i]); 
0
Nov 14 '11 at 17:49
source share

Here is an example with console.log instead of alert() . It is more comfortable:)

 var parse = function(){ var str = $('textarea').val(); var results = str.split("\n"); $.each(results, function(index, element){ console.log(element); }); }; $(function(){ $('button').on('click', parse); }); 

You can try here

0
Nov 14 '11 at 17:53
source share

 (function($) { $(document).ready(function() { $('#data').click(function(e) { e.preventDefault(); $.each($("#keywords").val().split("\n"), function(e, element) { alert(element); }); }); }); })(jQuery); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <textarea id="keywords">Hello World</textarea> <input id="data" type="button" value="submit"> 

-one
Sep 24 '19 at 3:29
source share



All Articles