Js learning notes (9) Regular Expressions

Source: Internet
Author: User
Tags character classes

1. Create a regular expression

1) Direct Volume creation method:

Var pattern =/s $ /; 

Like creating a string, you can use a pair of quotation marks. To create a regular expression, you can use a pair of slash signs (//)

2) object creation method:

Var pattern = new RegExp ("s $ ");// This formula is equivalent to the previous one.

Just as a String can be created through a String object, a regular expression can also be created through a RegExp object.

 

2. character classes

Put a separate amount of direct characters in square brackets and you can combine them into character classes. A character class matches any character contained in it, so the Regular Expression/[Abc]/Matches any of the letters a, B, and c.

You can also define a negative character class that matches any character that is not included in square brackets. When defining a negative character class, use a ^ symbol as the first character in square brackets, as shown in figure/[^ Abc]/Match any character except letters a, B, and c.

A character class can use a hyphen to represent a character range. For example, to match any number, you can use/[0-9]/, Which is equivalent/[0123456789]/To match any lowercase letter/[A-z]/To match any uppercase letters./[A-Z]/To match any number or letter/[A-zA-Z0-9]/

Predefine class

.

Any character except line breaks and carriage returns

\ D

Number

\ D

Non-numeric

\ W

Letter

\ W

Non-letter

\ S

White space characters

\ S

Non-blank characters

 

3. Repetition (simple quantifiers)

Duplicate characters appear after the pattern in which they are applied

{N, m} matches the first item at least n times, but cannot exceed m times, that is, n <= number of matches <= m

{N,} matches the first item at least n times, that is, n <= number of matches

{N} matches the first item EXACTLY n times, that is, n = number of matches

? Match the previous item 0 or 1 time

+ Match the previous item at least once

* Match the previous item 0 or multiple times

/\ D {2, 4}/match a 2-digit, 3-digit, or 4-digit

/\ W {3} \ d? /Match 3 or 3 Characters and add a number

Note:In use? And *, you must note that since they can all match 0 times, they are allowed to not match anything. For example,/a */actually matches the string "bbbb, because it contains 0

 

Greedy, inert, and supporting quantifiers:

Add one after a simple quantizer? ID, indicating a inert match, such *?; If this parameter is not added, it indicates greedy match. If the parameter is added with a plus sign (+), the entire string is matched, for example, * +.

Greedy match starts from the back to the front by deleting the last character one by one until the match is successful or all the characters are deleted.

The inert match is from the front to the back. It starts with one character and reads the strings one by one until the match is successful.

The dominant quantifiers match the entire string.

 

4. the character "|" is used to separate the selected characters.

For example,/AB | cd | ef/matches AB, cd, or ef.

/\ D {3} | [a-z] {4}/matched with 3 numbers or 4 lower-case letters

Note: The selected items are left-to-right. Once a match is found, the match will be stopped, even if there are other matching items.

5. Functions of brackets

1) Merge individual Project Groups into sub-expressions so that they can be used like processing an independent unit |, *, + ,? Such:

/Java (script )? /Matching java or javascript

/(AB | cd) + | ef/matches AB, cd, or ef

2) We can reference the content matched by the expression in the brackets at the back of the regular expression. This content can be determined only when a specific match is performed. The reference method is implemented by adding a digit or multiple-digit character after "\". The number specifies the position of the character expression with parentheses in the regular expression, for example, \ 1 references the Matching content of the first child expression in parentheses, and \ 2 references the second one. If there are nested parentheses, do not insert the child expressions without nesting, start with the number on the left to see the position of the left brace of the word expression with parentheses to be referenced in all the left brace. For example (a (B) (a), to reference (a), use \ 3

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.