JS regex to split by line
Solution 1:
arrayOfLines = lineString.match(/[^\r\n]+/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.
Solution 2:
Use
result = subject.split(/\r?\n/);
Your regex returns line1
twice because line1
is both the entire match and the contents of the first capturing group.