ES6 - 正則表示式全域性匹配



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

語法

RegExpObject.global        

返回值

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

示例

var re = new RegExp( "string" ); 
if ( re.global ) {
   console.log("Test1 - Global property is set");  
} else { 
   console.log("Test1 - Global property is not set");  
} 
re = new RegExp( "string", "g" ); 

if ( re.global ) { 
   console.log("Test2 - Global property is set");  
} else { 
   console.log("Test2 - Global property is not set");  
}          

輸出

Test1 - Global property is not set 
Test2 - Global property is set       
廣告