在命名组中获得多个结果

时间:2019-06-23 18:57:15

标签: javascript regex-group

我正在尝试使用命名组对正则表达式的结果进行分组。尽管设置了全局标志,但命名组仅获得第一个结果,并且进行字符串匹配可以获取所有匹配项,但无需对其进行分组。

我已经设置了g和m标志。

var REGEX = new RegExp(/^\s*(?<incorrect>.*\S*.*)\n*(?<corrected>.*\S*.*)[\r|\n]*/, "gm");
var mc = selectedText.match(REGEX);
var mc2 = REGEX.exec(selectedText);

我希望捕获的组中的结果是数组,而不仅仅是第一个匹配项。相反,我有这个:

mc: Array(3)
0: "maintain their brain and their mind open↵↵maintain an open mind↵↵↵↵"
1: "Our brain works to spend less energy than possible↵↵Our brain works to spend as little energy as possible↵↵↵↵"
2: "They brain is avoid to change↵↵Their brain avoids change"
length: 3

mc2: Array(3) 
0: "maintain their brain and their mind open↵↵maintain an open mind↵↵↵↵"
1: "maintain their brain and their mind open"
2: "maintain an open mind"
groups:
corrected: "maintain an open mind"
incorrect: "maintain their brain and their mind open"
index: 0
input: "maintain their brain and their mind open↵↵maintain an open mind↵↵↵↵Our brain works to spend less energy than possible↵↵Our brain works to spend as little energy as possible↵↵↵↵They brain is avoid to change↵↵Their brain avoids change"
length: 3

1 个答案:

答案 0 :(得分:1)

因此,看来.exec()您需要迭代地将相同的正则表达式对象应用于相同的字符串,直到它返回null,如下所示:

const text = "maintain their brain and their mind open\n\nmaintain an open mind\n\n\n\nOur brain works to spend less energy than possible\n\nOur brain works to spend as little energy as possible\n\n\n\nThey brain is avoid to change\n\nTheir brain avoids change";

var REGEX = new RegExp(/^\s*(?<incorrect>.*\S*.*)\n*(?<corrected>.*\S*.*)[\r|\n]*/, "gm");

let mc;
let mcAll = [];

while ((mc = REGEX.exec(text)) !== null) {
  mcAll.push(mc);
}

console.log(mcAll);

示例输出:

[ [ 'maintain their brain and their mind open\n\nmaintain an open mind\n\n\n\n',
    'maintain their brain and their mind open',
    'maintain an open mind',
    index: 0,
    input: 'maintain their brain and their mind open\n\nmaintain an open mind\n\n\n\nOur brain works to spend less energy than possible\n\nOur brain works to spend as little energy as possible\n\n\n\nThey brain is avoid to change\n\nTheir brain avoids change',
    groups: [Object: null prototype] {
      incorrect: 'maintain their brain and their mind open',
      corrected: 'maintain an open mind' } ],
  [ 'Our brain works to spend less energy than possible\n\nOur brain works to spend as little energy as possible\n\n\n\n',
    'Our brain works to spend less energy than possible',
    'Our brain works to spend as little energy as possible',
    index: 67,
    input: 'maintain their brain and their mind open\n\nmaintain an open mind\n\n\n\nOur brain works to spend less energy than possible\n\nOur brain works to spend as little energy as possible\n\n\n\nThey brain is avoid to change\n\nTheir brain avoids change',
    groups: [Object: null prototype] {
      incorrect: 'Our brain works to spend less energy than possible',
      corrected: 'Our brain works to spend as little energy as possible' } ],
  [ 'They brain is avoid to change\n\nTheir brain avoids change',
    'They brain is avoid to change',
    'Their brain avoids change',
    index: 176,
    input: 'maintain their brain and their mind open\n\nmaintain an open mind\n\n\n\nOur brain works to spend less energy than possible\n\nOur brain works to spend as little energy as possible\n\n\n\nThey brain is avoid to change\n\nTheir brain avoids change',
    groups: [Object: null prototype] {
      incorrect: 'They brain is avoid to change',
      corrected: 'Their brain avoids change' } ] ]