2

I have a list in a string, as below (from a <textarea> element with id=list):

q1

q2
q3


q4 

JavaScript:

var TA = document.getElementById("list").value; 
lines = TA.split("\r\n");

I want lines variable to have the values q1,q2,q3,q4, but the result is q1,,q2,q3,,,q4 (note the empty items)

I want to avoid the blank strings in the result array - how can I do that?

Kobi
  • 135,331
  • 41
  • 252
  • 292
kiran
  • 281
  • 1
  • 3
  • 12

4 Answers4

7

Try something like this, which should also take care of other formats of new lines (for example, only \n):

var lines = TA.split(/[\r\n]+/);

Or, to remove blank lines and spaces before/after your tokens (this one will only match the \r\n format, but you can use [\r\n] instead):

var lines = TA.split(/\s*\r\n\s*/);

Alternatively, you can match the lines instead of splitting the newlines:

var lines = TA.match(/.+/g);

. doesn't match new lines, so .+ is really matching a whole line, and skips empty lines. This will also skip empty tokens at the beginning or end of the array (as in \n\n a \n b), while split still creates empty elements.

Kobi
  • 135,331
  • 41
  • 252
  • 292
  • i want to match newlines, but except those new lines that are empty – kiran Apr 05 '11 at 11:50
  • @kiran - Try them, they should all work. If you're talking about the last option - that one is the best of the three! You *don't* want to match newlines at all - you want to match the **lines** `:)` – Kobi Apr 05 '11 at 11:53
  • 1
    Thanks, last one works wondefully in mozilla, but in IE its not working, i tested with IE7 – kiran Apr 05 '11 at 12:05
  • what change shld i made to wrk in IE – kiran Apr 05 '11 at 12:08
  • @Kiran - Weird, it is very basic and should. It's working for me in IE7 and even in IE6 (assuming `TA.value` is a string): http://jsbin.com/omaqi4 . Anyway, good luck! – Kobi Apr 05 '11 at 12:08
2

You could remove empty elements from the array after splitting it. See

Remove empty elements from an array in Javascript

Community
  • 1
  • 1
George
  • 4,273
  • 2
  • 15
  • 10
  • Thanks by using following jQuery code it works wonderfully var arr = [1,2,,3,,3,,,0,,,4,,4,,5,,6,,,,]; arr = $.grep(arr,function(n,i){ return(n); }); arr // [1, 2, 3, 3, 4, 4, 5, 6] – kiran Apr 05 '11 at 11:58
  • @kiran - note that the function is removing falsy values from the array - `0` was also removed! – Kobi Apr 05 '11 at 12:01
  • @kobi : Thaks i didnt noticed that, i wan to retain 0 also – kiran Apr 05 '11 at 12:07
1

Replace multiple \r\n by one before splitting

TA.replace(/(\r\n)+/g, "\r\n").split("\r\n")
Stefaan Colman
  • 3,715
  • 2
  • 22
  • 11
  • @steefan : it cant be all the case, possibly there can be double empty lines or triplr empty lines. – kiran Apr 05 '11 at 11:53
  • The regexp works for any number of newlines (+ stand for one or more). So double, tripple, or 100 empty lines, the regexp will replace them by one newline. – Stefaan Colman Apr 05 '11 at 12:09
0

Use var lines = TA.split(/[\r\n]{1,}/g);

Ashwin Krishnamurthy
  • 3,750
  • 3
  • 27
  • 49