📜  JavaScript RegExp toString()方法

📅  最后修改于: 2020-10-25 10:59:38             🧑  作者: Mango

JavaScript RegExp.prototype.toString()方法

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

句法

RegExpObj.toString();

参量

不适用

返回值

此方法返回表示给定对象的字符串。

例子1

var str = "";
   var re = new RegExp( "","g" );
         var result = re.toString(str);
         document.write("
Returned value : " + result);

输出:

Returned value : /(?:)/g

例子2

document.write (new RegExp("a+b+c","g").toString());

document.write(new RegExp("gt+yu").toString());

document.write(new RegExp("abcd").toString());

document.write(new RegExp('\\n', 'g').toString());

输出:

/a+b+c/g/
gt+yu//
abcd//
\n/g

例子3

var str = "scripting language";
 var re = new RegExp( "script", "g" );
           var result = re.toString(str);
         document.write("Returned value : " +  result); 

         re = new RegExp( "","g" );
          var result = re.toString(str);
         document.write("
Returned value : " + result);

输出:

Returned value : /script/g    
Returned value : /(?:)/g