--- title: RegExp.prototype.toString() slug: Web/JavaScript/Reference/Global_Objects/RegExp/toString tags: - JavaScript - Method - Prototype - RegExp translation_of: Web/JavaScript/Reference/Global_Objects/RegExp/toString ---
{{JSRef("Global_Objects", "RegExp")}}

概述

toString() 返回一个表示该正则表达式的字符串。

语法

regexObj.toString()

参数

描述

{{jsxref("Global_Objects/RegExp", "RegExp")}} 对象覆盖了 {{jsxref("Global_Objects/Object", "Object")}} 对象的 toString() 方法,并没有继承 {{jsxref("Object.prototype.toString()")}}。对于 RegExp 对象,toString 方法返回一个该正则表达式的字符串形式。

示例

例子:使用 toString

下例输出 RegExp 对象的字符串值:

myExp = new RegExp("a+b+c");
alert(myExp.toString());       // 显示 "/a+b+c/"

foo = new RegExp("bar", "g");
alert(foo.toString());         // 显示 "/bar/g"

规范

Specification Status Comment
ECMAScript 3rd Edition. Implemented in JavaScript 1.1 Standard Initial definition.
{{SpecName('ES5.1', '#sec-15.9.5.2', 'RegExp.prototype.toString')}} {{Spec2('ES5.1')}}  
{{SpecName('ES6', '#sec-regexp.prototype.tostring', 'RegExp.prototype.toString')}} {{Spec2('ES6')}}  

浏览器兼容性

{{ 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() }}

相关链接