JavaScript RegExp source Property

Beginner
5 min read
Updated: Nov 2026
3 examples
RegExp

Definition

In JavaScript, regex.source is a read-only string. It is the pattern text inside the slashes — without the flags.

Use it to inspect a pattern, log it for debugging, or build a new RegExp with different flags: new RegExp(old.source, "gi").

Syntax

Read .source on any RegExp. It never includes the flags.

JavaScript
const re = /\d+/gi;

console.log(re.source); // "\d+"
console.log(re.flags);  // "gi"
console.log(String(re)); // "/\d+/gi"
CallWhat it does
/cat/i.sourceReturns "cat".
/\d+/.sourceReturns "\d+" (as a string).
new RegExp(re.source, "g")Clone the pattern with new flags.

Remember this at a glance:

Remember
 /cat/i.source       → "cat"
 /\d+/.source        → "\d+"
 /a\\b/.source      → "a\\b"  (escaped backslashes)

source = pattern only
flags  = flag letters only
toString() = /pattern/flags

What You Get

NameDescription
regex.sourceRead-only string: the pattern without flags.
EscapesBackslashes appear escaped in the string the same way they do in the pattern.
Empty pattern(?:) may appear for an empty RegExp depending on how it was created.

Return Value

ValueDescription
Pattern stringThe body of the regular expression, without leading/trailing slashes or flags.

JavaScript Examples

Example 1 — Read source

Print the pattern text for a few regexes.

JavaScript
console.log(/cat/i.source);
console.log(/\d+/.source);
console.log(/a\.b/.source);
console.log(new RegExp("hi", "g").source);
Try It Yourself

How It Works

  1. Flags are not part of source.
  2. Digit and dot escapes stay visible in the string.
  3. Constructor patterns return the same kind of source text.

Example 2 — Clone with new flags

Keep the pattern, change the flags.

JavaScript
const base = /hello/i;
const every = new RegExp(base.source, "gi");

console.log(base.source);
console.log(every.flags);
console.log("Hello hello".match(every));
Try It Yourself

How It Works

  1. base.source is "hello".
  2. The new regex adds g while keeping ignoreCase via i.
  3. match with g returns every hit.

Example 3 — Debug pattern vs flags

Log both pieces when something does not match.

JavaScript
const re = /^\d+$/m;

console.log("pattern:", re.source);
console.log("flags:", re.flags);
console.log("multiline:", re.multiline);
console.log(re.test("12"));
Try It Yourself

How It Works

  1. Seeing source and flags side by side makes bugs easier to spot.
  2. Here m is on, so ^ and $ are per line.
  3. The digits-only pattern still matches "12".

Frequently Asked Questions

The pattern text without slashes or flags. For /cat/i, source is "cat".
source is the pattern. flags is the flag letters. Together they describe the full RegExp.
toString() returns something like /cat/i. source returns only cat.
No. Create a new RegExp instead.
In JavaScript strings, a single pattern backslash is written as \\. Logging source shows the escaped form.
Cloning a regex with new flags, logging patterns, or building a RegExp from another RegExp.

Next: lastIndex

Control where the next global or sticky search starts.

lastIndex tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

9 people found this page helpful