27

I have a textarea where the user can write up to 1000 characters. I need to get the jQuery('#textarea').val() and create an array where each item is a line of the textarea's value. That means:

This is a nice line inside the textarea.
This is another line.
(let's asume this line is empty - it should be ignored).
Someone left more than 2 new lines above.

Should be converted to a JavaScript array:

var texts = [];
text[0] = 'This is a nice line inside the textarea.';
text[1] = 'This is another line.';
text[2] = 'Someone left more than 2 new lines above.';

That way they can be easily imploded for to querystring (this is the qs format required by the provider):

example.com/process.php?q=["This is a nice line inside the textarea.","This is another line.","Someone left more than 2 new lines above."]

I tried both the phpjs explode() and string.split("\n") approaches but they doesn't take care of the extra new lines (aka line breakes). Any ideas?

2
  • Just off the top of my head, maybe try splitting it and then feeding the split bits into an array? Commented Dec 12, 2011 at 18:32
  • @AndrewPeacock i just updated the post with some info. I tried that with phpjs explode(), but it doesn't take care of the extra lines (if any) issue. Commented Dec 12, 2011 at 18:33

4 Answers 4

35

String.prototype.split() is sweet.

var lines = $('#mytextarea').val().split(/\n/);
var texts = [];
for (var i=0; i < lines.length; i++) {
  // only push this line if it contains a non whitespace character.
  if (/\S/.test(lines[i])) {
    texts.push($.trim(lines[i]));
  }
}

Note that String.prototype.split is not supported on all platforms, so jQuery provides $.split() instead. It simply trims whitespace around the ends of a string.

$.trim(" asd  \n") // "asd"

Check it out here: http://jsfiddle.net/p9krF/1/

Sign up to request clarification or add additional context in comments.

3 Comments

just one thing... one extra enter is allowed, two aren't (2 or more should be converted to one). any ideas?
Doesn't this snippet do that? If a line contains only whitespace it's ignored.
yes, but you may have up to 1 of those. this is handy for paragraphs separation.
7

Use split function:

var arrayOfLines = $("#input").val().split("\n");

1 Comment

should be .split(/\n/)
3
var split = $('#textarea').val().split('\n');
var lines = [];
for (var i = 0; i < split.length; i++)
    if (split[i]) lines.push(split[i]);
return lines;

1 Comment

worked and takes care of the extra spaces, thanks! just one thing, the "int" should be removed ;)
1

Try this

var lines = [];
$.each($('textarea').val().split(/\n/), function(i, line){
   if(line && line.length){
      lines.push(line);
   }
});

3 Comments

What is .each supposed to add? I mean, you're iterating over an array, pushing each element to a new array, and not using the original array. It doesn't look very useful.
I think now it makes senses, isn't it?
It does :) Though the line check is not necessary; you never get null or undefined.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.