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

Named capture groups - groups array not correct when using literal expressions #9722

Closed
kurt-genus opened this issue Mar 20, 2019 · 7 comments · Fixed by #9726
Closed

Named capture groups - groups array not correct when using literal expressions #9722

kurt-genus opened this issue Mar 20, 2019 · 7 comments · Fixed by #9726
Labels
Has PR i: bug outdated A closed issue/PR that is archived due to age. Recommended to make a new issue

Comments

@kurt-genus
Copy link

Bug Report

Current Behavior
If I have mutliple literal regex's, it seems to interfere with the groups array on the exec results.

Input Code

const regex1=/(?<alpha>\w+):(?<bravo>\d+)/;
const regex2=/(?<alpha>\w+):(?<bravo>[a-z]+):(?<charlie>\w+)/;

let foo='foo:abc:bar';
let result;

if(result= regex1.exec( foo ) ) {
  console.log( result);
} else if( result=regex2.exec(foo) ) {
  console.log( result );
}

Expected behavior/code
If you look at the code above, what I expect in the log is for the groups array to contain an entry for "charlie" because that is the expression which matched. However, this is what I see instead:

(4) ["foo:abc:bar", "foo", "abc", "bar", index: 0, input: "foo:abc:bar", groups: {…}]
0: "foo:abc:bar"
1: "foo"
2: "abc"
3: "bar"
groups: {alpha: "foo", bravo: "abc"}
index: 0
input: "foo:abc:bar"
length: 4
proto: Array(0)

result.groups.charlie is undefined even though result[3] is set to "bar".

Babel Configuration (.babelrc, package.json, cli command)

{
  "name": "genus-player",
  "version": "1.0.0",
  "description": "Media Player component for Genus MediaUpshot assets",
  "main": "dist/index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start": "webpack-dev-server --mode development",
    "build": "babel src -d dist --copy-files",
    "install": "babel src -d dist --copy-files"
  },
  "author": "",
  "license": "ISC",
  "peerDependencies": {
    "prop-types": "^15.7.2",
    "react": "^16.8.4",
    "react-dom": "^16.8.4",
    "react-pdf-js": "^4.0.2",
    "video.js": "^7.4.1"
  },
  "devDependencies": {
    "@babel/cli": "^7.2.3",
    "@babel/core": "^7.3.4",
    "@babel/plugin-proposal-class-properties": "^7.3.4",
    "@babel/preset-env": "^7.3.4",
    "@babel/preset-react": "^7.0.0",
    "@babel/register": "^7.0.0",
    "babel-loader": "^8.0.5",
    "css-loader": "^2.1.1",
    "html-webpack-plugin": "^3.2.0",
    "prop-types": "^15.7.2",
    "react": "^16.8.4",
    "react-dom": "^16.8.4",
    "react-pdf-js": "^4.0.2",
    "script-ext-html-webpack-plugin": "^2.1.3",
    "style-loader": "^0.23.1",
    "video.js": "^7.4.1",
    "webpack": "^4.29.6",
    "webpack-cli": "^3.2.3",
    "webpack-dev-server": "^3.2.1"
  }
}

Environment

  • Babel version(s): [e.g. v6.0.0, v7.0.0-beta.34]
  • Node/npm version:6.9
  • OS: linux
  • Monorepo: [e.g. yes/no/Lerna]
  • How you are using Babel: [e.g. cli, register, loader]

Possible Solution

Additional context/Screenshots
Add any other context about the problem here. If applicable, add screenshots to help explain.

@babel-bot
Copy link
Collaborator

Hey @kurt-genus! We really appreciate you taking the time to report an issue. The collaborators
on this project attempt to help as many people as possible, but we're a limited number of volunteers,
so it's possible this won't be addressed swiftly.

If you need any help, or just have general Babel or JavaScript questions, we have a vibrant Slack
community
that typically always has someone willing to help. You can sign-up here
for an invite.

@nicolo-ribaudo
Copy link
Member

What is your Babel config and what is the generated output?

@kurt-genus
Copy link
Author

kurt-genus commented Mar 20, 2019

.babelrc

{
    "presets": ["@babel/env", "@babel/react"],
    "plugins": ["@babel/plugin-proposal-class-properties"]
}

The generated output contains more than just this issue, I'll see if I can't get a complete working version.

But here is what I think is the relevant portion :

 var regex1 = _wrapRegExp(/(\w+):(\d+)/, {
        alpha: 1,
        bravo: 2
      });

      var regex2 = _wrapRegExp(/(\w+):([a-z]+):(\w+)/, {
        alpha: 1,
        bravo: 2,
        charlie: 3
      });

      var foo = 'foo:abc:bar';
      var result;

      if (result = regex1.exec(foo)) {
        console.log(result);
      } else if (result = regex2.exec(foo)) {
        console.log(result);
      }

@kurt-genus

This comment has been minimized.

@nicolo-ribaudo

This comment has been minimized.

@kurt-genus

This comment has been minimized.

@nicolo-ribaudo

This comment has been minimized.

@lock lock bot added the outdated A closed issue/PR that is archived due to age. Recommended to make a new issue label Jun 20, 2019
@lock lock bot locked as resolved and limited conversation to collaborators Jun 20, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Has PR i: bug outdated A closed issue/PR that is archived due to age. Recommended to make a new issue
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants