global 屬性

傳回布林值 (Boolean),該布林值表示規則運算式所使用之全域旗標 (g) 的狀態。

rgExp.global

引數

  • rgExp
    必要項。 規則運算式物件的執行個體。

備註

global 屬性 (Property) 是唯讀的,如果已設定規則運算式的全域旗標,就會傳回 true,否則會傳回 false。 預設值為 false

若使用全域旗標,表示搜尋功能應該在搜尋字串中將發生的每一次模式的項目找出來,而不只是只找出第一次。 這也稱為全域模式比對。

範例

下列範例說明 global 屬性的使用方式。 如果您將 g 傳入下列顯示的函式,則會將所有的單字 "the" 全部取代成 "a"。 請注意,因為您並未將 i (忽略大小寫) 旗標傳遞至該函式,所以不會取代字串開頭的 "The"。

這個函式顯示與可允許的規則運算式旗標 (gim) 相關聯的布林值。 。 此函式也會顯示已完成所有取代的字串。

function RegExpPropDemo(flag){
    // The flag parameter is a string that contains
    // g, i, or m.  The flags can be combined.

    // Check flags for validity.
    if (flag.match(/[^gim]/))
        {
        return ("Flag specified is not valid");
        }

    // Create the string on which to perform the replacement.
    var orig = "The batter hit the ball with the bat ";
    orig += "and the fielder caught the ball with the glove.";

    // Replace "the" with "a".
    var re = new RegExp("the", flag);
    var r = orig.replace(re, "a");        

    // Output the resulting string and the values of the flags.
    print ("global: " + re.global.toString());
    print ("ignoreCase: " + re.ignoreCase.toString());
    print ("multiline: " + re.multiline.toString());
    print ("Resulting String: " + r);
}

RegExpPropDemo("g");

下列是產生的輸出。

global: true
ignoreCase: false
multiline: false
Resulting String: The batter hit a ball with a bat and a fielder caught a ball with a glove.

需求

版本 5.5

套用至︰

規則運算式物件

請參閱

參考

ignoreCase 屬性

multiline 屬性

概念

規則運算式語法