Update: I have included @babel/plugin-transform-named-capturing-groups-regex, following the suggestion from @Jack Misteli to explore Babel.
I am attempting to comprehend what is causing this regular expression to break in Typescript (whether it's my configuration, the language itself, etc.).
Specifically, it seems to be related to the named capture groups I was using because removing them makes the code functional.
Original TS file:
/**
*
* @param {string} markdownLink A link in the form of [title](link description)
* @returns {object} An object with three keys: title, link, description
*/
export function parseLink(markdownLink: string) {
const pattern: RegExp = new RegExp(
/^(\[(?<title>[^\]]*)?\]\((?<link>[A-Za-z0-9\:\/\.\- ]+)(?<description>\"(.+)\")?\))/
)
const match = markdownLink.match(pattern)
const groups = match?.groups
return groups
}
...
My .babelrc
:
{
"presets": ["@babel/preset-env", "@babel/preset-typescript"],
"plugins": [
"@babel/plugin-proposal-class-properties",
"@babel/plugin-transform-runtime",
"@babel/plugin-transform-named-capturing-groups-regex"
]
}
...
If I remove the named capture groups, it does seem to work, though I'm now struggling to understand why I have a duplicate full capture group.
https://i.sstatic.net/UHuqt.png
...The debugger on the left shows the value of parsed
, which is the variable assigned to the return of the transpiled version.
So, here are my questions:
- Is there something I need to do to enable named capture groups in TS?
- Is there an explanation for why I'm receiving a duplicate group in the transpiled version?
- Is there a more efficient approach to handling this situation?
- What am I missing in configuring the named capture groups plugin that is preventing it from returning a match when present?