Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

JavaScript · Class

RegExp

RegExp represents a pattern for matching text. Create it with literal syntax or the constructor, then use it with RegExp and String matching methods.

How it works

RegExp represents a pattern used to search text. Create one with slash notation or the constructor; the constructor accepts a pattern and optional flags, and instances provide matching methods such as test() and exec().

Syntax

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

Parameters

NameTypeDescription
patternstring | RegExpThe pattern to compile, supplied as a string or a RegExp object.
flagsstringOptional flags that modify interpretation or interaction with input text.

Returns

A RegExp object representing the compiled pattern; in the special RegExp(pattern) case, the supplied RegExp may be returned directly.

Key points

  • RegExp packages a text pattern that can be applied to strings.
  • Use slash syntax for fixed patterns or the constructor when values must be built at runtime.
  • A literal places the pattern between slashes and appends any needed flags after the closing slash.
  • The constructor accepts either textual pattern data or an existing regular expression as its first argument.
  • Its second argument is an optional flags string controlling how the expression behaves.
  • Supplying a regex and flags constructs a regex using the source of the supplied expression and the requested flags.

Common mistakes

  • When a RegExp is supplied as the pattern and flags are also supplied, the new object uses the original source with the explicitly provided flags.

Example: Create and use a regular expression

const word = new RegExp("cat", "gi");

console.log(word.test("A CAT sleeps.")); // true
console.log(word.flags); // "gi"
console.log(word.source); // "cat"

Creates a case-insensitive, global regular expression and tests whether the input contains a matching word.

When to use it

Pattern validation

Validate or detect text patterns such as identifiers, tokens, or simple formats.

Text extraction

Extract matching portions and capture groups from input strings.

String processing

Perform replacements, searches, splitting, or iteration over matches.

Interview questions

QHow can a JavaScript RegExp be created?

RegExp objects describe text-matching patterns. They can be created with a literal such as /ab+c/g or with new RegExp(pattern, flags), which is useful when the pattern is assembled dynamically.

Follow-up: When is the constructor preferable to a literal? Use it when the pattern or flags come from runtime values; remember that string escaping rules apply before the regular-expression parser sees the pattern.

QWhat are the RegExp constructor parameters?

The first constructor argument is the pattern, which may be a string or another RegExp object. The optional second argument is a string containing flags that affect matching behavior.

Follow-up: What happens when a RegExp pattern is passed without new? If the pattern is already a RegExp and flags are undefined, the existing pattern may be returned directly; otherwise a new RegExp object is created.

QHow is a RegExp used for matching text?

A regular expression can be used with instance methods such as test() to check for a match or exec() to obtain match information. It also interoperates with String methods including match(), replace(), search(), and split().

Follow-up: Why can repeated test() calls differ for /x/g? Global matching advances lastIndex after successful matches, so later calls resume from the stored position.

  • RegExp.prototype.exec()
  • RegExp.prototype.test()
  • RegExp.prototype.compile()
  • RegExp.prototype.dotAll
  • RegExp.prototype.flags
  • RegExp.prototype.global
  • RegExp.prototype.hasIndices
  • RegExp.prototype.ignoreCase

Source: Examples | RegExp - JavaScript | MDN. Checked 2026-10-15.