RegExp.prototype.source

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.

概述

source 属性返回一个值为当前正则表达式对象的模式文本的字符串,该字符串不会包含正则字面量两边的斜杠以及任何的标志字符。

示例

使用 source

js
const regex = /fooBar/gi;

console.log(regex.source); // “fooBar”,不包含 /.../ 和“gi”。

空正则表达式和转义

js
new RegExp().source; // “(?:)”

new RegExp("\n").source === "\\n"; // true,从 ES5 开始

规范

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

浏览器兼容性

Report problems with this compatibility data on GitHub
desktopmobileserver
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
Deno
Node.js
source
"(?:)" for empty regexps
Line breaks and slashes are escaped
Prototype accessor property (ES2015)

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support

参见