Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / JavaScript

RegExp.prototype.source — regular expression pattern text

The source property returns a RegExp pattern's text without enclosing slashes or flags. Use it to inspect a pattern or reconstruct an expression with new RegExp(), passing its flags separately.

Version: ECMAScript and Web APIs; exact feature edition and browser support reviewed per entry; Node 24 in repository does not prove browser API support

Syntax

regexp.source

RegExp.prototype.source is an accessor property with a getter but no setter. Read it as regexp.source, without call parentheses. Its result is a string containing the pattern, without enclosing / delimiters or flags. Read flags separately through regexp.flags.

The result is suitable for representing the pattern inside a regular expression literal: slashes and line terminators are escaped appropriately. It therefore need not be identical to the text passed to the constructor. For an empty pattern, new RegExp("").source returns "(?:)", not an empty string. Reading RegExp.prototype.source itself also returns "(?:)".

To reconstruct the pattern and its flags, use new RegExp(regexp.source, regexp.flags). This creates a new object; it does not copy the lastIndex state. source does not turn arbitrary text into a safe literal pattern — existing regular expression operators retain their meaning.

The getter requires an actual RegExp object, with RegExp.prototype itself as a special exception. Calling the getter with another receiver throws TypeError. Assigning to the ordinary inherited source property does not change the pattern: it throws TypeError in strict mode and is ignored otherwise.

Examples

const pattern = /a\/b+/gi;
const copy = new RegExp(pattern.source, pattern.flags);

console.log(pattern.source);
console.log(pattern.flags);
console.log(copy.test("A/BBB"));
console.log(new RegExp("").source);

Result

a\/b+
gi
true
(?:)

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources