--- title: RegExp.prototype.source slug: Web/JavaScript/Reference/Global_Objects/RegExp/source tags: - JavaScript - Property - Prototype - Reference - RegExp - Regular Expressions translation_of: Web/JavaScript/Reference/Global_Objects/RegExp/source ---
{{JSRef("Global_Objects", "RegExp")}}

概述

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

示例

var regex = /fooBar/ig;

console.log(regex.source); // "fooBar",不包含 /.../ 和 "ig"。

规范

规范名称 规范状态 备注
ECMAScript 3rd Edition. Standard source 是正则对象自身的数据属性
{{SpecName('ES5.1', '#sec-15.10.7.1', 'RegExp.prototype.source')}} {{Spec2('ES5.1')}}  
{{SpecName('ES6', '#sec-get-regexp.prototype.source', 'RegExp.prototype.source')}} {{Spec2('ES6')}} source 成为了正则对象原型上的一个访问器属性

浏览器兼容性

{{CompatibilityTable}}
Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari
Basic support {{CompatVersionUnknown}} {{CompatVersionUnknown}} {{CompatVersionUnknown}} {{CompatVersionUnknown}} {{CompatVersionUnknown}}
Feature Android Chrome for Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
Basic support {{CompatVersionUnknown}} {{CompatVersionUnknown}} {{CompatVersionUnknown}} {{CompatVersionUnknown}} {{CompatVersionUnknown}} {{CompatVersionUnknown}}

相关链接