此页面由社区从英文翻译而来。了解更多并加入 MDN Web Docs 社区。
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 2015年7月.
source 属性返回一个值为当前正则表达式对象的模式文本的字符串,该字符串不会包含正则字面量两边的斜杠以及任何的标志字符。
In this article
示例
>使用 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® 2026 Language Specification> # sec-get-regexp.prototype.source> |