Movatterモバイル変換


[0]ホーム

URL:


MDN Web Docs

Modifier: (?ims-ims:...)

Limited availability

Amodifier overridesflag settings in a specific part of a regular expression. It can be used to enable or disable flags that change the meanings of certain regex syntax elements. These flags arei,m, ands.

Syntax

regex
(?flags1:pattern)(?flags1-flags2:pattern)

Note:JavaScript only has the "bounded" modifier form, where the pattern is placed inside the modifier group. Most other languages that support modifiers have an "unbounded" form, where the modifier is applied until the end of the closest containing group.

Parameters

flags1Optional

A string of flags to enable. Can contain any combination ofi,m, ands.

flags2Optional

A string of flags to disable. Can contain any combination ofi,m, ands, but must not contain any flags included inflags1.

pattern

A pattern consisting of anything you may use in a regex literal, including adisjunction.

Description

Some flags change the meanings of regex syntax elements:

  • Thei flag makes the regex case-insensitive by making allliteral characters andcharacter classes implicitly be lowercase.
  • Them flag changes the behavior ofinput boundary assertions^ and$ to match the start and end of each line, in addition to the start and end of the input string.
  • Thes flag changes the behavior of thewildcard. character to match any character, including line terminators.

Sometimes you may want these changes to only take effect in a specific part of a regex pattern. You can do so by wrapping that part in a modifier. For example:

js
/(?i:Hello) world/;

In this regex, thei flag is only enabled for theHello part of the pattern. Theworld part is case-sensitive. Therefore, it matchesHello world,hello world, andHELLO world, but notHELLO WORLD. The following is equivalent, by enabling thei flag globally, and then disabling it for theworld part:

js
/Hello (?-i:world)/i;

Theflags1 andflags2 parameters can contain any combination ofi,m, ands. However, the flags must all be unique betweenflags1 andflags2—you cannot enable or disable a flag twice, or enable a flag and then immediately disable it.

Theflags1 andflags2 parameters are optional, but at least one must be non-empty.(?flags1-:pattern) is a modifier that only enables flags (equivalent to(?flags1:pattern)).(?-flags2:pattern) is a modifier that only disables flags.(?:pattern) is just anon-capturing group, and(?-:pattern) is a syntax error.

Other flags don't make sense in a modifier and are thus syntax errors if included:

  • Theg andy flags determine how multiple calls toexec() behave and affect matching behavior of the whole regex.
  • Thed flag enables additional information in theexec() result and affects matching behavior of the whole regex.
  • Theu andv flags change the behavior of the regex engine in a way that's too complex to be locally modified. They also have global effects on the regex, such as how thelastIndex is advanced.

Examples

Matching a multiline format only at the start of the string

The following regex defines a format for a multiline string. The first^ represents the start of the whole input string, by being inside a(?-m:) modifier, while all other^ characters represent the start of a line:

js
const pattern = /(?-m:^)---\n^title:.*^slug:.*^---/ms;const input = `---title: "Modifier: (?ims-ims:...)"slug: Web/JavaScript/Reference/Regular_expressions/Modifier---`;pattern.test(input); // true// Extra line break at the start of stringconst input2 = `\n${input}`;pattern.test(input2); // false

Matching certain words case-insensitively

Imagine you are finding all variable declarations calledfoo orbar (because they are bad names). The word may appear in any case, but you know the keyword is always lowercase, so you can do this:

js
const pattern = /(?:var|let|const) (?i:foo|bar)\b/;pattern.test("let foo;"); // truepattern.test("const BAR = 1;"); // truepattern.test("Let foo be a number"); // false

Specifications

Specification
Unknown specification
# syntax

Browser compatibility

See also

Help improve MDN

Learn how to contribute.

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2025 Movatter.jp