Skip to main content

@babel/plugin-transform-regexp-modifiers

信息

本插件包含在 @babel/preset-envES2025 中。

¥This plugin is included in @babel/preset-env, in 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

该提案仅支持 ims 作为内联修饰符。

¥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