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 GitHubdesktop | mobile | server | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
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
The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.