Movatterモバイル変換


[0]ホーム

URL:


  1. Web
  2. JavaScript
  3. Reference
  4. Standard built-in objects
  5. RegExp
  6. RegExp()

RegExp() constructor

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since ⁨July 2015⁩.

TheRegExp() constructor createsRegExp objects.

For an introduction to regular expressions, read theRegular Expressions chapter in theJavaScript Guide.

Try it

const regex1 = /\w+/;const regex2 = new RegExp("\\w+");console.log(regex1);// Expected output: /\w+/console.log(regex2);// Expected output: /\w+/console.log(regex1 === regex2);// Expected output: false

Syntax

js
new RegExp(pattern)new RegExp(pattern, flags)RegExp(pattern)RegExp(pattern, flags)

Note:RegExp() can be called with or withoutnew, but sometimes with different effects. SeeReturn value.

Parameters

pattern

The text of the regular expression. This can also be anotherRegExp object.

flagsOptional

If specified,flags is a string that contains the flags to add. Alternatively, if aRegExp object is supplied for thepattern, theflags string will replace any of that object's flags (andlastIndex will be reset to0).

flags may contain any combination of the following characters:

d (indices)

Generate indices for substring matches.

g (global)

Find all matches rather than stopping after the first match.

i (ignore case)

When matching, casing differences are ignored.

m (multiline)

Treat beginning and end assertions (^ and$) as working over multiple lines. In other words, match the beginning or end ofeach line (delimited by\n or\r), not only the very beginning or end of the whole input string.

s (dotAll)

Allows. to match newlines.

u (unicode)

Treatpattern as a sequence of Unicode code points.

v (unicodeSets)

An upgrade to theu flag that enables set notation in character classes as well as properties of strings.

y (sticky)

Matches only from the index indicated by thelastIndex property of this regular expression in the target string. Does not attempt to match from any later indexes.

Return value

RegExp(pattern) returnspattern directly if all of the following are true:

  • RegExp() is called withoutnew;
  • pattern is a regex;
  • pattern.constructor === RegExp (usually meaning it's not a subclass);
  • flags isundefined.

In all other cases, callingRegExp() with or withoutnew both create a newRegExp object. Ifpattern is a regex, the new object'ssource ispattern.source; otherwise, its source ispatterncoerced to a string. If theflags parameter is notundefined, the new object'sflags is the parameter's value; otherwise, itsflags ispattern.flags (ifpattern is a regex).

Exceptions

SyntaxError

Thrown in one of the following cases:

  • pattern cannot be parsed as a valid regular expression.
  • flags contains repeated characters or any character outside of those allowed.

Examples

Literal notation and constructor

There are two ways to create aRegExp object: aliteral notation and aconstructor.

  • Theliteral notation takes a pattern between two slashes, followed by optional flags, after the second slash.
  • Theconstructor function takes either a string or aRegExp object as its first parameter and a string of optional flags as its second parameter.

The following three expressions create the same regular expression:

js
/ab+c/i;new RegExp(/ab+c/, "i"); // literal notationnew RegExp("ab+c", "i"); // constructor

Before regular expressions can be used, they have to be compiled. This process allows them to perform matches more efficiently. There are two ways to compile and get aRegExp object.

The literal notation results in compilation of the regular expression when the expression is evaluated. On the other hand, the constructor of theRegExp object,new RegExp('ab+c'), results in runtime compilation of the regular expression.

Use a string as the first argument to theRegExp() constructor when you want tobuild the regular expression from dynamic input.

Building a regular expression from dynamic inputs

js
const breakfasts = ["bacon", "eggs", "oatmeal", "toast", "cereal"];const order = "Let me get some bacon and eggs, please";order.match(new RegExp(`\\b(${breakfasts.join("|")})\\b`, "g"));// Returns ['bacon', 'eggs']

Specifications

Specification
ECMAScript® 2026 Language Specification
# sec-regexp-constructor

Browser compatibility

See also

Help improve MDN

Learn how to contribute

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2025 Movatter.jp