共用方式為


規則運算式程式設計

您可以在 JScript 中使用規則運算式來搜尋字串中的模式、取代文字,以及擷取子字串。

搜尋

下列 JScript 範例會找出一個字組的所有符合項目。

建立規則運算式的陳述式如下

var re = /\w+/g;

/\w+/ 模式指定要比對以下任何一個或多個字元:A-Z、a-z、0-9 和底線字元。 模式後面的 g (全域) 旗標會指定搜尋時應尋找所有符合模式的項目,而不只是第一個符合項目。

您也可以使用下列 JScript 替代語法。

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

為了擷取每個符合項目,exec 方法 會從 lastIndex 的位置開始持續搜尋,直到傳回 null 為止。

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
}

下列範例會尋找第一個符合項目: 由於未設定全域 (g) 旗標,因此搜尋會從搜尋字串開頭開始。

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
}

取代

下列範例會以 "a" 取代 "the" 項目。 由於未將 i (忽略大小寫) 旗標加入規則運算式旗標中,因此不會取代 "The" 的例項。

這個範例是使用 replace 方法

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

擷取子字串

在規則運算式模式中置入括號,即可建立能儲存供日後使用的子符合項目。

在下列範例中,模式包含三個子符合項目。 子符合項目會和每個符合項目一起顯示。

exec 方法 會傳回陣列, 此陣列的元素零包含完整的符合項目,而元素 1 至 n 則包含子符合項目。

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
}

旗標

在 JScript 規則運算式 /abc/gim 中,g 指定全域旗標,i 指定忽略大小寫旗標,而 m 則指定多行旗標。

下表顯示可以使用的旗標。

JScript 旗標

如果有旗標

g

尋找搜尋字串中模式的所有符合項目,而不只是第一個符合項目。

i

搜尋不區分大小寫。

m

^ 會比對緊接在 \n 或 \r 之後的位置,而

$ 則比對在 \n 或 \r 之前的位置。

不論是否有旗標,^ 都會比對搜尋字串開頭的位置,而 $ 則比對搜尋字串結尾的位置。

其他功能

以下列出其他可用的程式設計功能。

功能

描述

compile 方法 (Visual Studio - JScript)

將規則運算式編譯成內部格式,加快執行速度。

test 方法

測試模式是否在搜尋字串中出現。

search 方法

傳回第一個符合項目的位置。

請參閱

參考

規則運算式物件

概念

建立規則運算式

規則運算式語法