zoukankan      html  css  js  c++  java
  • 正则表达式编程

    可以在 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 + "	" + 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 + "	" + result[0]);
            }
    
        // Output:
        //  0-3 The
    }
    
    替换
     

    在以下示例中,“a”将替换“the”的匹配项。 但不会替换实例“The”,因为正则表达式标志中未包含 i(忽略大小写)标志。

    该示例使用 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 方法 将返回一个数组。 数组元素 0 包含了完整的匹配项,而元素 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
    }
    
    Flags
     

    在 JScript 正则表达式 /abc/gim 中,g 指示全局标志、i 指示忽略大小写标志,而 m 指示多行标志。

    下表显示了允许的标志。

     

    JScript 标志

    如果标志存在

    g

    查找搜索字符串中该模式的所有匹配项,而不仅仅是第一个匹配项。

    i

    搜索不区分大小写。

    m

    ^ 匹配   或   之后的位置,而

    $ 匹配   或   之前的位置。

    无论标志是否存在,^ 均匹配搜索字符串开头的位置,而 $ 均匹配搜索字符串结尾的位置。

    还可以使用以下附加编程功能。

     

    功能

    说明

    compile 方法 (Visual Studio - JScript)

    将正则表达式编译为内部格式,从而更快地执行。

    test 方法

    测试搜索字符串内是否存在模式。

    search 方法

    返回首个匹配项的位置。

  • 相关阅读:
    50个c/c++源代码网站
    VC 编译参数介绍
    基于Winsock API的VC网络编程实战
    notable
    The Beauty of Eventlet
    Transparent HTTP proxy
    用Python写一个本地Sogou代理服务器程序
    普林斯顿大学的计算机学课的作业
    HTML.py a Python module to easily generate HTML tables and lists
    Transparent HTTP proxy in python
  • 原文地址:https://www.cnblogs.com/milantgh/p/3637801.html
Copyright © 2011-2022 走看看