跳到主要内容

@babel/plugin-transform-regexp-modifiers

信息

This plugin is included in @babel/preset-env, in ES2025.

Example

i modifier

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

will be transformed to

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

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 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

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

Installation

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

Usage

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

Via CLI

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

Via Node.js API

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

References