Transform/runtime: Implement "named capturing groups"
See original GitHub issueParser support for named capturing groups has been added in ~issue #28~. It’s an ECMAScript proposal at stage 3.
To be able to use it in older engines, we can translate:
/(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})/
Into:
new RegExpTree(/(\d{4})-(\d{2})-(\d{2})/, ['year', 'month', 'day']);
The regexpTree
creates a lightweight facade wrapper on top of ES6-compatible regexp, and provides the same API (test
, exec
methods, etc).
class RegExpTree {
constructor(re, groups) {
this.re = re;
this.groups = groups;
}
test(string) {
return this.re.test(string);
}
exec(string) {
const result = this.re.exec(string);
// process `result`, and attach `groups`:
// result.groups.year -> result[1]
// result.groups.month -> result[2]
// etc.
}
...
}
As well as overrides String.prototype.match
method to attach group names to the groups
property on the match result.
const originalMatch = String.prototype.match;
String.prototype.match = function(regexp) {
// Simple regexp.
if (!(regexp instanceof RegExpTree) {
return originalMatch.call(this, regexp);
}
// RegExpTree.
return regexp.exec(this);
};
Issue Analytics
- State:
- Created 6 years ago
- Comments:5 (5 by maintainers)
Top Results From Across the Web
babel/plugin-transform-named-capturing-groups-regex
NOTE: This plugin is included in @babel/preset-env , in ES2018 NOTE: This plugin generates code that needs ES6 regular expressions functionalities.
Read more >website/plugin-transform-named-capturing-groups-regex.md ...
NOTE: This plugin is included in @babel/preset-env , in ES2018 NOTE: This plugin generates code that needs ES6 regular expressions functionalities.
Read more >@babel/plugin-transform-named-capturing-groups-regex - npm
Compile regular expressions using named groups to ES5. ... Start using @babel/plugin-transform-named-capturing-groups-regex in your project ...
Read more >@babel/plugin-transform-named-capturing-groups-regex | Yarn
Fast, reliable, and secure dependency management.
Read more >Typescript Regex - Named Capture Groups Breaks?
The answer is to use @babel/plugin-transform-named-capturing-groups-regex; Is there something I should know to understand why I'm getting a ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
yes, named groups count => x=1 y=3.
you can also use
[null, 1, "year", 3, "month", "day"]
, then you simply loop through the array and storenames[i]: originalMatch[i]
in matches.groups, So,matches.groups["3"]
would result in “y”.Local runtime support has been added in https://github.com/DmitrySoshnikov/regexp-tree/commit/0e6a9ff70679b15bc475574b2ab361bddadc2d84.