RegExp.prototype.toString()

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.

概述

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

语法

regexObj.toString()

参数

描述

RegExp 对象覆盖了 Object 对象的 toString() 方法,并没有继承 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
ECMAScript® 2025 Language Specification
# sec-regexp.prototype.tostring

浏览器兼容性

Report problems with this compatibility data on GitHub
desktopmobileserver
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
Deno
Node.js
toString
Line breaks and slashes are escaped

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support

参见