Skip to main content

@babel/plugin-proposal-regexp-modifiers

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 apply multiple modifiers:

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

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

Installation

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

Usage

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

Via CLI

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

Via Node.js API

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

References