Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[RegExp] Named Capture groups #521

Closed
kalvenschraut opened this issue Apr 1, 2019 · 8 comments
Closed

[RegExp] Named Capture groups #521

kalvenschraut opened this issue Apr 1, 2019 · 8 comments

Comments

@kalvenschraut
Copy link

@kalvenschraut kalvenschraut commented Apr 1, 2019

I am receiving an unexpected qualifier error in IE 11 using the following expression which is coming from the RegExp constructor polyfill.

const pathRegex = /^(?<schema>[-a-z]+)\.(?<table>[-a-z]+)\.(?<fieldPath>[a-zA-Z_[\].0-9]+)$/; 
const regex =  RegExp(`^${pathRegex.source.substring(1,pathRegex.source.length - 1)}#(?<bindingNumber>[0-9]+)$`);

I wanted to use pathRegex by itself but the normal regex variable is suppose to be made up of the pathRegex variable. Right now I can work around this by copying it over, but I would prefer to use the variable to construct the whole regex.

I am using core-js@3 and babel-preset-env@7.4.2
I must say the usage flag recently introduced is making my job of maintaining IE 11 compatibility a lot easier!

@AlokTakshak
Copy link

@AlokTakshak AlokTakshak commented Jun 21, 2019

@zloirock I would like to contribute , can you tell me what needs to be done?

Loading

@zloirock
Copy link
Owner

@zloirock zloirock commented Jun 24, 2019

@AlokTakshak you could find some basic info here. As an example, you could take a look at Number constructor wrapper for handling modern number literals.

Loading

@zloirock
Copy link
Owner

@zloirock zloirock commented Jun 24, 2019

If you have any more detailed questions - just ask me.

Loading

@zloirock
Copy link
Owner

@zloirock zloirock commented Jun 26, 2019

RegExp constructor should parse patterns with NCG syntax and return a regex created from pattern allowed in the old version of the language with internal metadata about NCG. RegExp.prototype.exec should be patched for creating groups property with the required result.

Loading

@zloirock
Copy link
Owner

@zloirock zloirock commented Jun 26, 2019

Seems something like here https://github.com/commenthol/named-regexp-groups, but in the main class.

Loading

@zloirock
Copy link
Owner

@zloirock zloirock commented Jun 29, 2019

  1. Not exactly - as I wrote, it should return a real regex instance, not something regex-like. It's just an example. Also, you could take a look at related babel transform output and the helper
  2. It theoretically could require changes not only on RegExp.prototype.exec, but also in some other methods, for example, on RegExp.prototype[@@replace], but I don't think that on match since String methods related RegExp should call related RegExp methods and they should call RegExp.prototype.exec under the hood. See the spec.

Loading

@shtelzerartem
Copy link

@shtelzerartem shtelzerartem commented Apr 26, 2021

@zloirock Due to inactivity, I've decided to make a quick dive into this issue.
I started working on patching regexp-exec.js.

To check if named groups are not supported:

var NCG_INCLUDED = /(.?)/.exec('').groups !== undefined;

.? is actually to aviod problems with eslint (don't blame for it pls), another way we can use it empty with long eslint-disable-next-line

After that, it goes to extracting group names from the regexp source, deleting them from there, as after we can make matching with match results by indexes.
But building the regexp query for that comes with trouble because group name is an object property name and should be validated accordingly. You can use any character to name a property, for example, in Hebrew /(?<משאוה>)/ or russian (?<чтото>). In the example you have sent before there is limit for Latin symbols, but it doesn't feel right.
I found this project here, but wanted to ask you for an advice, maybe I'm missing smth?

Would be happy to hear your thoughts about it!

Loading

@zloirock
Copy link
Owner

@zloirock zloirock commented Apr 28, 2021

By my understanding, es.regexp.constructor should contain detection support of NCG (or it should be in a separate helper), if they are not supported it should extract named groups and transform regexp to supported syntax. If NCG are not supported, regexp-exec side should evaluate regexp and match the result with the extract groups. About language characters - I don't know, I didn't check those examples, I'll do it when I'll have enough time.

Loading

@zloirock zloirock added the es label May 30, 2021
@zloirock zloirock changed the title Named Capture groups in RegExp constructor [RegExp] Named Capture groups Jun 8, 2021
@zloirock zloirock closed this in 913625f Jun 20, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
4 participants