Reputation: 209
I am trying to group the results of a regular expression using named groups. The named groups only get the first result, despite the global flag being set, and doing a string match can get all matches, but without grouping them.
I have set the g and m flags.
var REGEX = new RegExp(/^\s*(?<incorrect>.*\S*.*)\n*(?<corrected>.*\S*.*)[\r|\n]*/, "gm");
var mc = selectedText.match(REGEX);
var mc2 = REGEX.exec(selectedText);
I would have expected the results in the captured groups to be arrays, and not only the first matches. Instead, I have this:
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
Upvotes: 1
Views: 64
Reputation: 2367
So, it seems with .exec()
you need iteratively apply same regex object to the same string until it returns null
, like this:
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);
Sample output:
[ [ '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' } ] ]
Upvotes: 1