JS regex按行拆分。

时间:2022-03-28 22:58:02

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

你如何将一长段文字分成几行?为什么这个返回line1两次?

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

["line1", "line1"]

[" 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 \r\n line endings.

我希望使用regex拆分而不是字符串。拆分,因为我将处理Linux \n和Windows \r\n行结束。

6 个解决方案

#1


93  

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.

正如蒂姆所说,这是整个比赛和捕捉。它看起来是regex.exec(string)返回,不管全局修饰符是什么,都返回了第一个匹配项。

#2


78  

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.

您的regex将返回line1两次,因为line1是整个匹配和第一个捕获组的内容。

#3


18  

I am assuming following constitute newlines

我假设以下是新行。

  1. \r followed by \n
  2. \ r \ n紧随其后
  3. \n followed by \r
  4. r \ n \紧随其后
  5. \n present alone
  6. \ n现在独自一人
  7. \r present alone
  8. \ r现在独自一人

Please Use

请使用

var re=/\r\n|\n\r|\n|\r/g;

arrayofLines=lineString.replace(re,"\n").split("\n");

for an array of all Lines including the empty ones.

对于包含空的所有行的数组。

OR

Please Use

请使用

arrayOfLines = lineString.match(/[^\r\n]+/g); 

For an array of non empty Lines

对于一系列非空行。

#4


14  

Even simpler regex that handles all line ending combinations, even mixed in the same file, and removes empty lines as well:

更简单的regex处理所有行结束组合,甚至混合在同一个文件中,并删除空行:

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

var = text.split行(/[\ r \ n]+ / g);

With whitespace trimming:

空格调整:

var lines = text.trim().split(/\s*[\r\n]+\s*/g);

var = text.trim行().split(/ \ s *[\ r \ n]+ \ s * / g);

#5


6  

First replace all \r\n with \n, then String.split.

首先将所有的\r\n替换为\n,然后字符串。

#6


0  

http://jsfiddle.net/uq55en5o/

http://jsfiddle.net/uq55en5o/

var lines = text.match(/^.*((\r\n|\n|\r)|$)/gm);

var = text.match行(/ ^。*((\ r \ n \ n | | \ r)| $)/通用);

I have done something like this. Above link is my fiddle.

我做过类似的事情。上面的链接是我的小提琴。

#1


93  

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.

正如蒂姆所说,这是整个比赛和捕捉。它看起来是regex.exec(string)返回,不管全局修饰符是什么,都返回了第一个匹配项。

#2


78  

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.

您的regex将返回line1两次,因为line1是整个匹配和第一个捕获组的内容。

#3


18  

I am assuming following constitute newlines

我假设以下是新行。

  1. \r followed by \n
  2. \ r \ n紧随其后
  3. \n followed by \r
  4. r \ n \紧随其后
  5. \n present alone
  6. \ n现在独自一人
  7. \r present alone
  8. \ r现在独自一人

Please Use

请使用

var re=/\r\n|\n\r|\n|\r/g;

arrayofLines=lineString.replace(re,"\n").split("\n");

for an array of all Lines including the empty ones.

对于包含空的所有行的数组。

OR

Please Use

请使用

arrayOfLines = lineString.match(/[^\r\n]+/g); 

For an array of non empty Lines

对于一系列非空行。

#4


14  

Even simpler regex that handles all line ending combinations, even mixed in the same file, and removes empty lines as well:

更简单的regex处理所有行结束组合,甚至混合在同一个文件中,并删除空行:

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

var = text.split行(/[\ r \ n]+ / g);

With whitespace trimming:

空格调整:

var lines = text.trim().split(/\s*[\r\n]+\s*/g);

var = text.trim行().split(/ \ s *[\ r \ n]+ \ s * / g);

#5


6  

First replace all \r\n with \n, then String.split.

首先将所有的\r\n替换为\n,然后字符串。

#6


0  

http://jsfiddle.net/uq55en5o/

http://jsfiddle.net/uq55en5o/

var lines = text.match(/^.*((\r\n|\n|\r)|$)/gm);

var = text.match行(/ ^。*((\ r \ n \ n | | \ r)| $)/通用);

I have done something like this. Above link is my fiddle.

我做过类似的事情。上面的链接是我的小提琴。