Friday, May 17, 2024
 Popular · Latest · Hot · Upcoming
85
rated 0 times [  86] [ 1]  / answers: 1 / hits: 115088  / 14 Years ago, thu, february 17, 2011, 12:00:00

How do you split a long piece of text into separate lines? Why does this return line1 twice?



/^(.*?)$/mg.exec('line1rnline2rn');



[line1, line1]




I turned on the multi-line modifier to make ^ and $ match beginning and end of lines. I also turned on the global modifier to capture all lines.



I wish to use a regex split and not String.split because I'll be dealing with both Linux n and Windows rn line endings.


More From » regex

 Answers
51
arrayOfLines = lineString.match(/[^rn]+/g);


As Tim said, it is both the entire match and capture. It appears regex.exec(string) returns on finding the first match regardless of global modifier, wheras string.match(regex) is honouring global.


[#93686] Wednesday, February 16, 2011, 14 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
karleel

Total Points: 309
Total Questions: 79
Total Answers: 86

Location: Monaco
Member since Sun, Jan 16, 2022
2 Years ago
;