How to use JavaScript Regular Expressions

Source: Internet
Author: User
Tags character classes

Topic: JavaScript Regular Expression

Define a regular expression:

Var reg_pattern = new RegRxt ("a \ d"); // indicates that a is followed by a number.

Var reg_pattern =/a \ d /;

Regular Expression methods related to String objects

Name

Description

Example

Macth (regExp)

The specified regular expression is used to search for a string. If a function is found and an array is returned, Null is returned.

Test.html

Replacre (regExp, replaceText)

Replace all the substrings in the original string that match the regular expression regExp with replaceText to return a new string.

Test.html

Search (regExp)

Search for a string. Unlike macth (regExp), only the index of the position that appears during the first match is returned. Otherwise,-1 is returned.

Character class:

Character List: A character list consisting of one or more characters in square brackets.

All are considered to be matched. For example, [abc] indicates matching any one of a, B, or c. However, each match can only match one character in the list, instead of multiple

Reverse list: the reverse list matches all characters other than the specified characters in the list. The reverse list is added before the list

The "^" symbol is defined. For example, [^ abc] indicates that all characters except a, B, and c are matched.

Character range: for consecutive character lists, such as [1234567] or [abcdefg], you can use a simple

Is called the character range. Use the "-" symbol to start and end characters in the character range.

To indicate all characters between the start character and the end character. For example, [a-z] indicates ~ Z

All lowercase characters

Reverse range: add the "^" symbol before the character to match any character beyond the character range.

And define character classes

Character class

Description

·

Match any single character except the line break \ n and the carriage return character, which is equivalent to [^ \ n \ r]

\ D

Matches a number, which is equivalent to [0-9].

\ D

Match a non-numeric character, equivalent to [^ 0-9]

\ W

Match any single character that contains underlines, including ~ Z, ~ Z, 0 ~ 9 and lower slide line "_", equivalent to [a-zA-Z0-9 _]

\ W

Matches any non-single character, equivalent to [^ a-zA-Z0-9 _]

\ S

Matches any Unicode white space characters, including spaces, tabs, and page breaks. It is equivalent to [\ f \ t \ n \ r].

\ S

Match any non-blank characters

\ B

Backspace

Repeated times (quantifiers)

*: Indicates zero or multiple matches to the previous expression.

For example:

Var reg_pattern =/bo */;

Match the following strings:

B

Bo

Booo

+: Match of the preceding expression appears one or more times consecutively.

For example:

Var reg_pattern =/bo + /;

Match the following strings:

Bo

Booo

{N}: n is a non-negative integer. Indicates that the match of the previous expression appears n times.

For example:

Var reg_pattern =/bo {3 }/;

Only "booo" can be matched ".

{N ,}: n is a non-negative integer. Indicates that the previous expression must be matched at least n times, that is, n times.

Or more than n times.

For example:

Var reg_pattern =/[A-Z] {3,}/g;

Match three or more consecutive uppercase letters.

{N, m}: n and m are non-negative integers. N <m. It indicates that at least n matches and at most m matches.

Or more than n times.

Expression:

Var reg_pattern =/[A-Z] {3, 5}/g;

Match 3 to 5 consecutive uppercase letters.

For example, search for the regular expression of the phone number starting with "133:

Var reg_pattern =/133 \ d {8}/g;

Boundary:

^: Indicates the start position of a string. In multi-row matching, it indicates the start position of a row. Symbol and

Does not match any character

$: Indicates the end position of a string. In multi-row matching, it indicates the end position of a row. Symbol and

Does not match any character

\ B: represents the boundary of a word (rather than a character), that is, the position between a word and a space,


Author: chow _ zh

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.