A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from http://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Regular_expressions/Modifier below:

Modifier: (?ims-ims:...) - JavaScript | MDN

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

Limited availability

A modifier overrides flag 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 are i, m, and s.

Syntax
(?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
flags1 Optional

A string of flags to enable. Can contain any combination of i, m, and s.

flags2 Optional

A string of flags to disable. Can contain any combination of i, m, and s, but must not contain any flags included in flags1.

pattern

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

Description

Some flags change the meanings of regex syntax elements:

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:

In this regex, the i flag is only enabled for the Hello part of the pattern. The world part is case-sensitive. Therefore, it matches Hello world, hello world, and HELLO world, but not HELLO WORLD. The following is equivalent, by enabling the i flag globally, and then disabling it for the world part:

The flags1 and flags2 parameters can contain any combination of i, m, and s. However, the flags must all be unique between flags1 and flags2—you cannot enable or disable a flag twice, or enable a flag and then immediately disable it.

The flags1 and flags2 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 a non-capturing group, and (?-:pattern) is a syntax error.

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

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:

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 string
const input2 = `\n${input}`;

pattern.test(input2); // false
Matching certain words case-insensitively

Imagine you are finding all variable declarations called foo or bar (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:

const pattern = /(?:var|let|const) (?i:foo|bar)\b/;

pattern.test("let foo;"); // true
pattern.test("const BAR = 1;"); // true
pattern.test("Let foo be a number"); // false
Specifications Browser compatibility See also

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4