JavaScript 正则表达式 - toString


描述

toString方法以正则表达式文字的形式返回正则表达式的字符串表示形式

句法

其语法如下 -

RegExpObject.toString();

返回值

返回正则表达式的字符串表示形式。

例子

<html>
   
   <head>
      <title>JavaScript RegExp toString Method</title>
   </head>
   
   <body>
      <script type = "text/javascript">
         var str = "Javascript is an interesting scripting language";
         var re = new RegExp( "script", "g" );
         
         var result = re.toString(str);
         document.write("Test 1 - returned value : " +  result); 
         
         re = new RegExp( "/", "g" );
         
         var result = re.toString(str);
         document.write("<br />Test 2 - returned value : " +  result); 
      </script>
   </body>
</html>

输出

Test 1 - returned value : /script/g
Test 2 - returned value : /\//g