Movatterモバイル変換


[0]ホーム

URL:


MDN Web Docs

RegExp.prototype.source

BaselineWidely available

Thesource accessor property ofRegExp instances returns a string containing the source text of this regular expression, without the two forward slashes on both sides or any flags.

Try it

const regex1 = /fooBar/gi;console.log(regex1.source);// Expected output: "fooBar"console.log(new RegExp().source);// Expected output: "(?:)"console.log(new RegExp("\n").source === "\\n");// Expected output: true (starting with ES5)// Due to escaping

Description

Conceptually, thesource property is the text between the two forward slashes in the regular expression literal. The language requires the returned string to be properly escaped, so that when thesource is concatenated with a forward slash on both ends, it would form a parsable regex literal. For example, fornew RegExp("/"), thesource is\\/, because if it generates/, the resulting literal becomes///, which is a line comment. Similarly, allline terminators will be escaped because line terminatorcharacters would break up the regex literal. There's no requirement for other characters, as long as the result is parsable. For empty regular expressions, the string(?:) is returned.

Examples

Using source

js
const regex = /fooBar/gi;console.log(regex.source); // "fooBar", doesn't contain /.../ and "gi".

Empty regular expressions and escaping

js
new RegExp().source; // "(?:)"new RegExp("\n").source === "\\n"; // true, starting with ES5

Specifications

Specification
ECMAScript® 2026 Language Specification
# sec-get-regexp.prototype.source

Browser compatibility

See also

Help improve MDN

Learn how to contribute.

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2025 Movatter.jp