javascript - JS regex to split by line -


how split long piece of text separate lines? why return line1 twice?

/^(.*?)$/mg.exec('line1\r\nline2\r\n'); 

["line1", "line1"]

i turned on multi-line modifier make ^ , $ match beginning , end of lines. turned on global modifier capture all lines.

i wish use regex split , not string.split because i'll dealing both linux \n , windows \r\n line endings.

arrayoflines = linestring.match(/[^\r\n]+/g); 

as tim said, both entire match , capture. appears regex.exec(string) returns on finding first match regardless of global modifier, wheras string.match(regex) honouring global.


Comments

Popular posts from this blog

apache - Add omitted ? to URLs -

redirect - bbPress Forum - rewrite to wwww.mysite prohibits login -

php - How can I stop spam on my custom forum/blog? -