question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Transform/runtime: Implement "named capturing groups"

See original GitHub issue

Parser 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:closed
  • Created 6 years ago
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
hg42commented, Apr 12, 2017

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 store names[i]: originalMatch[i] in matches.groups, So, matches.groups["3"] would result in “y”.

0reactions
DmitrySoshnikovcommented, Apr 15, 2017
Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found