One of the new Javascript Regular Expressions: Basic knowledge of JavaScript Regular Expressions

Source: Internet
Author: User

I haven't read the regular expression for a long time. I happen to use it today. I just learned how to read a book.
I used to send regular expressions for beginners without worry. Now I have added the Group to provide download improvements.

I. Basic knowledge about Javascript Regular Expressions

1. 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)

2. 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]);
}

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

These are the most basic methods for using JS regular expressions. If you cannot see them, copy them to your notebook and read the next article.

Regular Expressions are grouping knowledge.

 

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.