@babel/plugin-proposal-duplicate-named-capturing-groups-regex
示例
输入
JavaScript
var re = /(?<year>\d{4})-(?<month>\d{2})|(?<month>\d{2})-(?<year>\d{4})/;
console.log(re.exec("02-1999").groups.year);
输出
JavaScript
var re = _wrapRegExp(/(\d{4})-(\d{2})|(\d{2})-(\d{4})/, {
year: [1, 4],
month: [2, 3],
});
console.log(re.exec("02-1999").groups.year);
安装
- npm
- Yarn
- pnpm
npm install --save-dev @babel/plugin-proposal-duplicate-named-capturing-groups-regex
yarn add --dev @babel/plugin-proposal-duplicate-named-capturing-groups-regex
pnpm add --save-dev @babel/plugin-proposal-duplicate-named-capturing-groups-regex
用法
使用配置文件(推荐)
babel.config.json
{
"plugins": ["@babel/plugin-proposal-duplicate-named-capturing-groups-regex"]
}
通过 CLI
Shell
babel --plugins @babel/plugin-proposal-duplicate-named-capturing-groups-regex script.js
通过 Node API
JavaScript
require("@babel/core").transformSync("code", {
plugins: ["@babel/plugin-proposal-duplicate-named-capturing-groups-regex"],
});
选项
runtime
boolean
,默认为 true
禁用此选项后,Babel 不会使用 _wrapRegExp
辅助函数包装正则表达式。输出仅支持内部组引用,不支持运行时属性
JavaScript
var stringRe = /(?:(?<quote>")|(?<quote>')).*?\k<quote>/;
stringRe.test("'foo'"); // "true", works
stringRe.exec("'foo'").groups.quote; // undefined
提示
您可以在此处阅读有关配置插件选项的更多信息