Commit 1edc51f7 authored by Diegodlh's avatar Diegodlh
Browse files

Improve capturing group support

- If one or more capturing groups are defined, do not return full match
- Ignore optional capturing groups not found in the match
- Support capturing groups with global flag
parent e75d4126
......@@ -301,12 +301,22 @@ export class MatchTransformation extends Transformation {
if (!this.itemwise) {
input = [input.join()];
}
const output: StepOutput = input.reduce((matches: StepOutput, item) => {
const match = item.match(this._target);
if (match !== null) {
matches = matches.concat(match);
const output: StepOutput = input.reduce((output: StepOutput, item) => {
let matches;
if (!this._target.global) {
const match = item.match(this._target);
matches = match !== null ? [match] : [];
} else {
matches = Array.from(item.matchAll(this._target));
}
for (let matchArray of matches) {
// drop full match if using capturing groups
if (matchArray.length > 1) matchArray.shift();
// drop optional capturing groups not found
matchArray = matchArray.filter((matchItem) => matchItem !== undefined);
output = output.concat(matchArray);
}
return matches;
return output;
}, []);
return Promise.resolve(output);
}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment