Export (0) Print
Expand All

Regular Expression Programming

Visual Studio 2010

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

The following JScript 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 JScript syntax.

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

To retrieve each match, the exec Method 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)
    {
        print (result.index + "-" + result.lastIndex + "\t" + 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 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)
        print ("not found");
    else
        {   
        print (result.index + "-" + result.lastIndex + "\t" + result[0]);
        }

    // Output:
    //  0-3 The
}

In the following example, 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.

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

    print(result);

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

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. Element zero of the array contains the complete match, and the elements 1 through n contain the submatches.

function SearchWithSubmatches()
{
    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)
    {
        print ("e-mail address: " + result[0]);

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

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

    // 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
}

In the JScript 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.

JScript flag

If flag is present

g

Find all occurrences of the pattern 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.

The following additional programming features are available.

Feature

Description

compile Method (Visual Studio - JScript)

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

test Method

Test whether a pattern occurs in a searched string.

search Method

Return the position of the first match.

Community Additions

ADD
Show:
© 2014 Microsoft