Skip to main content

@babel/plugin-transform-regexp-modifiers

info

这个插件包含在 @babel/preset-env 中,在 ES2025。

示例​

🌐 Example

i 修饰符​

🌐 i modifier

input.js
// matches Aa and aa
const regex = /(?i:a)a/

will be transformed to

output.js
const regex = /(?:[Aa])a/

m 修饰符​

🌐 m modifier

input.js
// matches aa, a\naa, etc. but not a\na
const regex = /(?m:^a)a/

will be transformed to

output.js
const regex = /(?:(?:^|(?<=[\n\r\u2028\u2029]))a)a/

s 修饰符​

🌐 s modifier

input.js
// matches \na and aa, but not \n\n
const regex = /(?s:.)./

will be transformed to

output.js
const regex = /(?:[\s\S])./;

多个修饰符​

🌐 Multiple modifiers

你还可以启用多个修饰符:

🌐 You can also enable multiple modifiers:

// matches Aa, aa, A\naa, etc. but not A\na
const regex = /(?im:^a)a/

或禁用它们:

🌐 or disable them:

// matches Aa, aa, A\naa, etc. but not A\na
const regex = /^a(?-im:a)/im

这个提案只支持 i、m 和 s 作为内联修饰符。

🌐 This proposal only supports i, m and s as inline modifiers.

安装​

🌐 Installation

npm install --save-dev @babel/plugin-transform-regexp-modifiers

用法​

🌐 Usage

🌐 With a configuration file (Recommended)

babel.config.json
{
"plugins": ["@babel/plugin-transform-regexp-modifiers"]
}

通过 CLI​

🌐 Via CLI

Shell
babel --plugins @babel/@babel/plugin-transform-regexp-modifiers script.js

通过 Node.js API​

🌐 Via Node.js API

JavaScript
require("@babel/core").transformSync(code, {
plugins: ["@babel/plugin-transform-regexp-modifiers"],
});

参考​

🌐 References