Regexp constructors transform

This changes RegExp constructors into literals if the RegExp arguments are strings

示例

输入

const foo = 'ab+';
var a = new RegExp(foo+'c', 'i');

输出

const foo = 'ab+';
var a = /ab+c/i;

安装

npm install babel-plugin-transform-regexp-constructors

用法

通过 .babelrc(Recommended)

.babelrc

{
  "plugins": ["transform-regexp-constructors"]
}

通过 CLI

babel --plugins transform-regexp-constructors script.js

通过 Node API

require("babel-core").transform("code", {
  plugins: ["transform-regexp-constructors"]
});