← Back to release summary

RegExp modifiers

Category
JavaScript
Type
Chromium catches up
Status
Enabled by default (Chrome 125)
Intent stage
None

WPT details: In test262: https://github.com/tc39/test262/pull/3960

Summary

RegExp modifiers adds the ability to locally modify the 'i', 'm', and 's' flags inside a pattern. To enable a flag for a subexpression, use `(?X:subexpr)` where X is one of 'i', 'm', or 's'. To disable a flag for a subexpression, use `(-X:subexpr)`. For example, for the case-insensitivity 'i' flag: ``` const re1 = /^[a-z](?-i:[a-z])$/i; re1.test("ab"); // true re1.test("Ab"); // true re1.test("aB"); // false const re2 = /^(?i:[a-z])[a-z]$/; re2.test("ab"); // true re2.test("Ab"); // true re2.test("aB"); // false ```

Motivation

JS RegExp flags currently apply to an entire pattern. Being able to apply the flags locally to a subexpression gives more flexibility to writing regular expressions, and also brings JS up to par with other regular expression engines that already have the expressivity.

Standards & signals

Ecosystem

View on chromestatus.com