Compartir a través de


Regular Expression Programming (JavaScript)

You can use regular expressions in JavaScript to search for patterns in a string, replace text, and extract substrings.

Searching

The following JavaScript example finds all occurrences of a word.

The statement that creates the regular expression is

var re = /\w+/g;

The /\w+/ pattern specifies to match one or more of any of the following characters: A-Z, a-z, 0-9, and the underscore character. The g (global) flag that follows the pattern specifies that the search should find all occurrences of the pattern instead of just the first occurrence.

You can also use the following alternative JavaScript syntax.

var re = new RegExp("\\w+", "g");

To retrieve each match, the exec Method (JavaScript) keeps searching, starting at the position of lastIndex, until null is returned.

function SearchGlobal() {
    var src = "The quick brown fox jumps over the lazy dog.";

    // Create a regular expression pattern that has a global flag.
    var re = /\w+/g;

    var result;

    // Get the first match.
    result = re.exec(src);
    while (result != null) {
        // New line:
        document.write ("<br />");  
        document.write (result.index + "-" + result.lastIndex + " ");
        document.write (result[0]);

        // Get the next match.
        // Because the global flag is set, the search starts at the
        // position of lastIndex.
        result = re.exec(src);
    }

    // Output:
    //  0-3 The
    //  4-9 quick
    //  10-15 brown
    //  16-19 fox
    //  20-25 jumps
    //  26-30 over
    //  31-34 the
    //  35-39 lazy
    //  40-43 dog
}

The following example finds just the first match. Because the global (g) flag is not set, the search starts from the start of the search string.

function SearchNonGlobal() {
    var src = "The quick brown fox jumps over the lazy dog.";

    // Create a regular expression pattern that does not have
    // a global flag.
    var re = /\w+/;

    // Get the first match.
    // Because the global flag is not set, the search starts
    // from the beginning of the string.
    var result = re.exec(src);

    if (result == null)
        document.write ("not found");
    else {   
        document.write (result.index + "-" + result.lastIndex + " ");
        document.write (result[0]);
    }

    // Output:
    //  0-3 The
}

Replacing Text

In the following examples, occurrences of "the" are replaced with "a". The instance of "The" is not replaced, because the i (ignore case) flag is not included in the regular expression flags.

The example uses the replace Method (JavaScript).

function ReplaceGlobal() {
    var src = "The batter hit the ball with the bat ";
    src += "and the fielder caught the ball with the glove.";

    // Replace "the" with "a".
    var re = /the/g;
    var result = src.replace(re, "a");

    document.write(result);

    // Output:
    //  The batter hit a ball with a bat and a fielder caught a ball with a glove.
}

Extracting Substrings

Placing parentheses in a regular expression pattern creates a submatch that can be stored for later use.

In the following example, the pattern includes three submatches. The submatch strings display together with each match. An array is returned by the exec Method (JavaScript). Element zero of the array contains the complete match, and the elements 1 through n contain the submatches.

function SearchWithSubmatches() {
    var newline = "<br />";
    var s = "";
    var result;

    var src = "Please send mail to george@contoso.com and someone@example.com. Thanks!";

    // Create a regular expression to search for an e-mail address.
    // Include the global flag.
    // (More sophisticated RegExp patterns are available for
    // matching an e-mail address.)
    var re = /(\w+)@(\w+)\.(\w+)/g;

    // Get the first match.
    result = re.exec(src);
    while (result != null) {
        s += newline;  
        s += "e-mail address: " + result[0];

        // Get the submatched parts of the address.
        s += newline;
        s += "user name: " + result[1];
        s += newline;
        s += "host name: " + result[2];
        s += newline;
        s += "top-level domain: " + result[3];
        s += newline;  

        // Get the next match.
        result = re.exec(src);
    }

    document.write (s);

    // Output:
    //  e-mail address: george@contoso.com
    //  user name: george
    //  host name: contoso
    //  top-level domain: com

    //  e-mail address: someone@example.com
    //  user name: someone
    //  host name: example
    //  top-level domain: com
}

Flags

In the JavaScript regular expression /abc/gim, the g specifies the global flag, the i specifies the ignore case flag, and the m specifies the multiline flag.

The following table shows the allowed flags.

JavaScript flag

If flag is present

g

All occurrences of the pattern are found in the searched string instead of just the first occurrence.

i

The search is case-insensitive.

m

^ matches positions following a \n or \r, and

$ matches positions before \n or \r.

Whether or not the flag is present, ^ matches the position at the start of the searched string, and $ matches the position at the end of the searched string.

Additional Features

The following additional programming features are available.

Feature

Description

compile Method (JavaScript)

Compile a regular expression into an internal format for faster execution.

test Method (JavaScript)

Test whether a pattern occurs in a searched string.

search Method (JavaScript)

Return the position of the first match.

See Also

Reference

Regular Expression Object (JavaScript)

Concepts

Creating a Regular Expression (JavaScript)

Regular Expression Syntax (JavaScript)