最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

jquery - Splitting text in textarea by new lines (including empty lines) into javascript array - Stack Overflow

programmeradmin5浏览0评论

I'm trying to split the text inside Splitting textarea data by new lines. My current code works, except for a small requirement: The resulting array must include empty lines as well.

<script>
$(function(){
    var lines = [];
    $.each($('#data').val().split(/\n/), function(i, line){
        if(line){
            lines.push(line);
        }
    });
    console.log(lines);
});
</script>


<textarea id="data">
I like to eat icecream. Dogs are fast.

The previous line is composed by spaces only.



The last 3 lines are empty.

One last line.
</textarea>

The current result is:

["I like to eat icecream. Dogs are fast.", " ", "The previous line is composed by spaces only.", "The last 3 lines are empty.", "One last line."]

What it should be:

["I like to eat icecream. Dogs are fast.", " ", "The previous line is composed by spaces only.", "", "", "", "The last 3 lines are empty.", "", "One last line."]

I'm trying to split the text inside Splitting textarea data by new lines. My current code works, except for a small requirement: The resulting array must include empty lines as well.

<script>
$(function(){
    var lines = [];
    $.each($('#data').val().split(/\n/), function(i, line){
        if(line){
            lines.push(line);
        }
    });
    console.log(lines);
});
</script>


<textarea id="data">
I like to eat icecream. Dogs are fast.

The previous line is composed by spaces only.



The last 3 lines are empty.

One last line.
</textarea>

The current result is:

["I like to eat icecream. Dogs are fast.", " ", "The previous line is composed by spaces only.", "The last 3 lines are empty.", "One last line."]

What it should be:

["I like to eat icecream. Dogs are fast.", " ", "The previous line is composed by spaces only.", "", "", "", "The last 3 lines are empty.", "", "One last line."]

Share Improve this question edited Jan 30, 2015 at 18:11 Andres SK asked Jan 30, 2015 at 18:02 Andres SKAndres SK 11k27 gold badges96 silver badges158 bronze badges 2
  • 2 Does the simple split not do what you ask? Your each function removes all empty lines. – HBP Commented Jan 30, 2015 at 18:12
  • @HBP I didn't know that \n wouldn't return false/null. It has been fixed as per the accepted answer. – Andres SK Commented Jan 30, 2015 at 20:20
Add a comment  | 

2 Answers 2

Reset to default 11

Your .split will include \n, but when line is falsey you can just push an empty string...

$(function(){
    var lines = [];
    $.each($('#data').val().split(/\n/), function(i, line){
        if(line){
            lines.push(line);
        } else {
            lines.push("");
        }
    });
    console.log(lines);
});

Here is a working example : JSFiddle

Output:

["I like to eat icecream. Dogs are fast.", 
"",  "The previous line is composed by spaces only.",  
"",  "",  "", 
"The last 3 lines are empty.",  
"",  "One last line."]

Or simply as comment above suggests (I had assumed that your example had been simplified and you need to do something else in the .each loop):

var lines = $('#data').val().split(/\n/);

JS Fiddle

In addition to geedubb's answer if you prefer to avoid using the backslash for encoding reasons:

var lines = $('#data').val().split(String.fromCharCode(10));
发布评论

评论列表(0)

  1. 暂无评论