RegExp

RegExp.prototype.source

Returns a string containing the source text of the regex pattern, without the forward slashes or flags

Syntax

JavaScript
regexp.source

Return Value

A string representing the pattern of the regular expression

Examples

Basic Usage
const regex = /hello\s+world/gi;
console.log(regex.source); // 'hello\s+world'
console.log(regex.flags); // 'gi'
Practical Example
const regex = /^[a-z]+$/i;
console.log(regex.source); // '^[a-z]+$'
console.log(regex.flags); // 'i'
Advanced Usage
const regex = new RegExp('\\d{2,4}', 'g');
console.log(regex.source); // '\d{2,4}'

Understanding RegExp.prototype.source

The RegExp.prototype.source method in JavaScript returns a string containing the source text of the regex pattern, without the forward slashes or flags. It belongs to the RegExp object and is one of the most widely used methods for working with regexp values in modern JavaScript and TypeScript applications.

The method signature is regexp.source. When called, it returns a string representing the pattern of the regular expression. Understanding when and how to use source() helps you write more expressive, readable code.

Common use cases for RegExp.prototype.source include data transformation, input validation, API response processing, and building reusable utility functions. It works well alongside related methods like regexp-tostring, regexp-test, regexp-exec, enabling you to chain operations together for complex data manipulation pipelines.

Browser support for RegExp.prototype.source is excellent across all modern browsers including Chrome, Firefox, Safari, and Edge. It is also fully supported in Node.js and Deno. For older environments, transpilation with Babel or a polyfill may be needed.

Browser Compatibility

Supported in all modern browsers (Chrome, Firefox, Safari, Edge) and Node.js. Part of the ECMAScript standard.

Related Methods

More RegExp Methods

Other methods in the RegExp object

Related Tools

More RegExp Methods

Explore JavaScript Methods

Browse our complete reference of 410 JavaScript methods with syntax, examples, and explanations.