Regular Expression in Javascript

Source: Internet
Author: User

Regular Expression are very important in professional JavaScript.

There is a wonderful
Resource in Mozilla Developer Center.

Two ways to construct a regular expression

1. Literal expression

 
Re =/AB + C/g;

2. constructor Function

 
Re = new Regexp ("AB + C", "G ");

Possible flags

1. G-global search.

2. I-case-insensitive search.

3. M-multi-line search.

Simple and special characters

A regular expression is composed of simple characters (such as/ABC123 _/) and special
Characters.

Some important special characters:

1. ^-matches beginning of input.

2. $-matches end of input.

3. *-matches the preceding character 0 or more times.

4. +-matches the preceding character 1 or more times.

5 .? -Matches the preceding character 0 or 1 time.

6 ..-matches any single character.

Sepcial character \

There are two meanings of the special charater.

1. used before simple character, indicating that the next character is special.

E.g. B is a simple character, \ B is used to indicate a word
Boundary.

Some important special character \:

1. \ B-matches a word boundary.

2. \ D-matches a digit character.

3. \ s-matches a white space character, including space,
Tab, line feed.

4. \ W-matches any alphanumeric character including
Underscore. equivalent to [A-Za-z0-9 _].

Javascript funtions to operate regular expression

1. Exec-returns: array.

2. Match-returns: array or null.

3. Test-returns: true or false.

4. Search-returns: Index or-1.

5. replace-returns: string.

Examples:

 
/D (B +) D/g.exe C ("cdbbdbsbz"); // ["dbbd", "BB"] "cdbbdbsbz ". match (/d (B +) D/g); // ["dbbd"] "cdbbdbsbz ". search (/d (B +) D/g); // 1/D (B +) D/g. test ("cdbbdbsbz"); // true

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.