JavaScript - RegExp 全域性屬性



描述

global 是 RegExp 物件的只讀布林屬性。它指定特定正則表示式是否執行全域性匹配,即它是否使用“g”屬性建立。

語法

語法如下:

RegExpObject.global

返回值

如果設定了“g”修飾符,則返回“TRUE”,否則返回“FALSE”。

示例

嘗試以下示例程式。

<html>   
   <head>
      <title>JavaScript RegExp global Property</title>
   </head>
   
   <body>      
      <script type = "text/javascript">
         var re = new RegExp( "string" );
         
         if ( re.global ) {
            document.write("Test1 - Global property is set"); 
         } else {
            document.write("Test1 - Global property is not set"); 
         }
         re = new RegExp( "string", "g" );
         
         if ( re.global ) {
            document.write("<br />Test2 - Global property is set"); 
         } else {
            document.write("<br />Test2 - Global property is not set"); 
         }
      </script>   
   </body>
</html>

輸出

Test1 - Global property is not set
Test2 - Global property is set
javascript_regexp_object.htm
廣告