Home JavaScript RegExp source JavaScript RegExp source Property 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.
const re = /\d+/gi;
console.log(re.source); // "\d+"
console.log(re.flags); // "gi"
console.log(String(re)); // "/\d+/gi" Call What 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:
/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 Name Description 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 Value Description Pattern stringThe body of the regular expression, without leading/trailing slashes or flags.
JavaScript Examples 1. Read source 2. Clone with new flags 3. Debug pattern vs flags Example 1 — Read source Print the pattern text for a few regexes.
console.log(/cat/i.source);
console.log(/\d+/.source);
console.log(/a\.b/.source);
console.log(new RegExp("hi", "g").source); How It Works Flags are not part of source. Digit and dot escapes stay visible in the string. Constructor patterns return the same kind of source text. Example 2 — Clone with new flags Keep the pattern, change the flags.
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)); hello
gi
[ 'Hello', 'hello' ] How It Works base.source is "hello".The new regex adds g while keeping ignoreCase via i. match with g returns every hit.Example 3 — Debug pattern vs flags Log both pieces when something does not match.
const re = /^\d+$/m;
console.log("pattern:", re.source);
console.log("flags:", re.flags);
console.log("multiline:", re.multiline);
console.log(re.test("12")); pattern: ^\d+$
flags: m
multiline: true
true How It Works Seeing source and flags side by side makes bugs easier to spot. Here m is on, so ^ and $ are per line. The digits-only pattern still matches "12". Frequently Asked Questions What does RegExp.source return? The pattern text without slashes or flags. For /cat/i, source is "cat".
How is source different from flags? source is the pattern. flags is the flag letters. Together they describe the full RegExp.
How is source different from toString()? toString() returns something like /cat/i. source returns only cat.
Can I assign regex.source = "new"? No. Create a new RegExp instead.
Why do I see double backslashes? In JavaScript strings, a single pattern backslash is written as \\. Logging source shows the escaped form.
When should I use source? Cloning a regex with new flags, logging patterns, or building a RegExp from another RegExp.
CODETOFUN NEWS
Stay Ahead in Tech Get the latest IT news, programming updates, and technology trends directly on your phone.
About the author Developer, cloud engineer, and technical writer
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
Helpful Share Copy link Suggestion