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 Language Specification
# sec-get-regexp.prototype.source

浏览器兼容性

BCD tables only load in the browser

参见