Basic knowledge about Javascript Regular Expressions

Source: Internet
Author: User
  • Create and use a JavaScript Regular object

    Declare a JavaScript Regular Expression
    VaR recat = new Regexp ("cat ");
    You can also
    VaR recat =/CAT/; // Perl style (recommended)

  • Learn the most common test exec match search replace split six methods

    1) test checks whether the specified string exists

    VaR DATA = "123123 ";
    VaR recat =/123/GI;
    Alert (recat. Test (data); // true

    // Check whether the character g exists. continue to the next step. I is case insensitive.

    2) exec returns the query value

    VaR DATA = "123123,213,123, 312, 3, Cat, Cat, dsfsdfs ,";
    VaR recat =/CAT/I;
    Alert(recat.exe C (data); // cat

    3) match to get the query Array

    VaR DATA = "123123,213,123, 312, 3, Cat, Cat, dsfsdfs ,";
    VaR recat =/CAT/GI;
    VaR arrmactches = data. Match (recat)

    For (VAR I = 0; I <arrmactches. length; I ++ ){
    Alert (arrmactches [I]); // Cat
    }

    4) The returned search location is similar to indexof.

    VaR DATA = "123123,213,123, 312, 3, Cat, Cat, dsfsdfs ,";
    VaR recat =/CAT/GI;
    Alert (data. Search (recat); // 23

    5) replace replacement characters with regular expressions

    VaR DATA = "123123,213,123, 312, 3, Cat, Cat, dsfsdfs ,";
    VaR recat =/CAT/GI;
    Alert (data. Replace (recat, "libinqq "));

    6) Split splits the array using regular expressions.

    VaR DATA = "123123,213,123, 312, 3, Cat, Cat, dsfsdfs ,";
    VaR recat = /\,/;
    VaR arrdata = data. Split (recat );

    For (VAR I = 0; I <arrdata. length; I ++ ){
    Alert (arrdata [I]);
    }

  • Learning simple class negative class range class combination class
    // Simple class
    VaR DATA = "1 libinqq, 2 libinqq, 3 libinqq, 4 libinqq ";
    VaR recat =/[123] libinqq/GI;
    VaR arrdata = data. Match (recat );

    For (VAR I = 0; I <arrdata. length; I ++)
    {
    Alert (arrdata [I]); // 1 libinqq 2 libinqq 3 libinqq
    }

    // Negative
    VaR DATA = "alibinqq, 1 libinqq, 2 libinqq, 3 libinqq, 4 libinqq"; // \ u0062cf
    VaR recat =/[^ A123] libinqq/GI;
    VaR arrdata = data. Match (recat );

    For (VAR I = 0; I <arrdata. length; I ++)
    {
    Alert (arrdata [I]); // 4 libinqq
    }

    // Range
    VaR DATA = "libinqq1, libinqq2, libinqq3, libinqq4, libinqq5"; // \ u0062cf
    VaR recat =/libinqq [2-3]/GI;
    VaR arrdata = data. Match (recat );

    For (VAR I = 0; I <arrdata. length; I ++)
    {
    Alert (arrdata [I]); // libinqq2 libinqq3
    }

    // Combination
    VaR DATA = "a, B, c, W, 1, 2, 3, 5"; // \ u0062cf
    VaR recat =/[a-q1-4 \ n]/GI;
    VaR arrdata = data. Match (recat );

    For (VAR I = 0; I <arrdata. length; I ++)
    {
    Alert (arrdata [I]); // a B C 1 2 3
    }

  • Cited by: http://blog.myspace.cn/e/405771220.htm

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.