Regular Expression, Regular Expression

Source: Internet
Author: User

Regular Expression, Regular Expression
Zookeeper

1. What is a regular expression?
① File path wildcard
"? "Matches a single character." * "matches zero or multiple characters.
② Regular Expression
Expressions that comply with certain rules
③ Match metacharacters with sub-Scripts
Metacharacters
Child match
The expression enclosed in parentheses is a child match (child expression)

2. RegExp object
① Create
1) display constructor: var regExp = new RegExp ("pattern" [, "flags"]);
2) Implicit constructor:/pattern/[flags]

Flags flag characters:
G: Used as a global sign ()
I: used to ignore case sensitivity
M: Used as a multi-row flag

Note:
When the display constructor is used, replace "\" with "\" for each "\" in the original regular expression mode.
Var re = new RegExp ("\ d {5 }");
Var re =/\ d {5 }/;
② RegExp object attributes:
1) Static attributes:
Index: the starting position of the match. The default value is-1.
Input: returns the currently used string.
LastIndex: the next position of the matching character. The default value is-1.
LastMatch: The last matched string
LastParen: The last matched string of the Child match.
LeftContext: Left content of the last matched string
RightContext: the content on the right of the last matched string
$1... $9 matched content

2) instance attributes:
Global: global flag
IgnoreCase: case insensitive
Multiline: multiline flag,
Source: return mode text
③ Method
Test (str): checks whether a string meets the regular expression, returns TRUE or false, and updates static attributes.
Exec (str): checks whether a string meets the regular expression, returns the search array or null, and updates the static attribute.
The returned array has three attributes: index, input, and lastIndex.
Compile ("pattern" [, "flags"]): replace the regular expression used by the RegExp object instance.

Note: If a global sign (g) is set for the regular expression, you can call the exec and test Methods multiple times to perform continuous search in the string,
Each time, the string is searched from the location specified by the lastIndex attribute of the RegExp object. If the global flag is not set, exec and
The test method ignores the lastIndex attribute value of the RegExp object and starts searching from the starting position of the string.

④ Methods related to regular expressions of String objects
Match (RegExp): returns an array containing search results, similar to the exec method.
Search (RegExp): returns the position of the first matched string.
Replace (RegExp, replaceText): replace a matching string and return the replaced string.
Split ([separator [, limit]): returns the split array.
⑤ Regular expression syntax
Metacharacters:
1). Qualifier: used to specify the number of consecutive occurrences of the character or combination item before it
N + matches any string containing at least one n.
N * matches any string containing zero or more n.
N? Match any string containing zero or one n.
N {X} matches the string that contains X n sequences.
N {X, Y} matches the string that contains X or Y n sequences.
N {X,} matches strings that contain at least X n sequences.
N $ matches any string ending with n.
^ N matches any string starting with n.
? = N matches any string followed by the specified string n.
?! N matches any string that is not followed by the specified string n.
2). Select a match character (|)
3). Group Composite: a symbol that combines a part of the regular expression.
4). Reverse quote: Used to match the identifier of the content captured by the previous Group combination.
(Pattern): child matching, buffering, called with reverse references
\ Num: num indicates the serial number of the group combination.
(? : Pattern): child matching, but not stored in the buffer, cannot be called with a reverse reference character
5). Special characters
. Find a single character, except for line breaks and line terminator.
\ W: Search for word characters.
\ W searches for non-word characters.
\ D.
\ D: searches for non-numeric characters.
\ S.
\ S.
\ B searches for matching at the beginning or end of a word.
\ B searches for matches that are not at the beginning or end of a word.
\ 0: Search for NUL characters.
\ N.
\ F.
\ R.
\ T.
\ V.
\ Xxx: Search for characters specified by Octal numbers xxx.
\ Xdd: Search for characters specified by dd in hexadecimal format.
\ Uxxxx searches for Unicode characters specified by the hexadecimal number xxxx.
6). Character matching character
[Abc] searches for any character between square brackets.
[^ Abc] searches for any characters that are not in square brackets.
[0-9] search for any number from 0 to 9.
[A-z] searches for any characters from lowercase to lowercase.
[A-Z] looks for any character from uppercase A to uppercase Z.
[A-z] searches for any character from uppercase A to lowercase z.
[Adgk] searches for any character in a given set.
[^ Adgk] searches for any character outside the given set.
(Red | blue | green.



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.