Share via


Proprietà global

Restituisce un valore booleano che indica lo stato del flag globale (g) utilizzato con un'espressione regolare.

rgExp.global

Argomenti

  • rgExp
    Obbligatoria. Istanza di un oggetto Regular Expression.

Note

La proprietà global è in sola lettura e restituisce true se il flag globale è impostato per un'espressione regolare, false in caso contrario. Il valore predefinito è false.

Quando utilizzato, il flag globale indica che in una ricerca devono essere individuate tutte le occorrenze del criterio nella stringa da cercare e non solo la prima. È noto anche come corrispondenza globale.

Esempio

Nell'esempio riportato di seguito viene illustrato l'utilizzo della proprietà global. Se si passa g nella funzione illustrata di seguito, tutte le istanze della parola "the" verranno sostituite dalla parola "a". Si noti che la parola "The" all'inizio della stringa non viene sostituita in quanto il flag i (che consente di ignorare la distinzione tra maiuscole e minuscole) non viene passato alla funzione.

Questa funzione visualizza i valori booleani associati ai flag dell'espressione regolare consentiti, ovvero g, i e m. La funzione inoltre visualizza la stringa con tutte le sostituzioni apportate.

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");

Di seguito è riportato l'output risultante.

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.

Requisiti

Versione 5.5

Si applica a:

Oggetto Regular Expression

Vedere anche

Riferimenti

Proprietà ignoreCase

Proprietà multiline

Concetti

Sintassi delle espressioni regolari