Common Regular Expressions Keywords
"." : Any character "*": Any number "+": Any number, at least one "?": 0 -1 "\d"digit"\w": Any alphanumeric, underlined word"\s": Any blank, space, tab, line break, at least one space\: Escape character []: match some characters -: For example 0-5, from several to several ^: arbitrary non-character \s: all non-whitespace {4}: Character occurrences, etc. 4{2,4}: Characters appear 2-4 times {2}: At least 2 $: the bounds of the string \b: the boundary of the word, the front border or the back boundary ^ and $ are used together, are matched initially and finally (|) The regular expression group \1 the reverse reference, and the regular expression remembers the part (? =\s) Assertion syntax, matching left or right, qualifying
Second, JS is used in the common syntax of expressions:
The above example is a regular expression of a common way, the regular expression in JS main use scenario is:
1, test: Check whether the string conforms to the required format, mainly used in the verification time, the keyword is pattern.test (str), the return result is true, or false
var // Defining Regular Expressions var // Defining Strings // verifying strings using regular Expressions
2. Exec: Gets the content in the string that matches the regular expression
var // regular expressions, matching starts with a space, with the contents of the <> tag var str= ' // test string alert (pattern.exec (str)); <aa> #ab, <ab>
According to () grouping, the match result is an array a[],a[0] is the longest match to the string, A[1] is the first array, a[2] is the second parenthesis matches the content, and so on, if a small parenthesis within the match does not appear in the array, you need to add: filter, the specific meaning of reference?: Usage.
3. Replace: Local or global replacement of data
var str= ' 8google8 8google8 8google8 '; var // var// non-greedy mode with replacement result <strong>google</strong> 8google8 8google8 var// alert (str.replace (pattern, ' <strong>$1</strong> ') );
Replaces the string matching part with the specified string, which can be grouped according to the use of: the part of the source string is reserved.
4. Match, similar to the Exec use method, but Exec is a regular method, match is the method of STR, so the use of str.match (pattern), the other difference temporarily does not explain
5. Search, using
/* use Search to find matching data */ var pattern =/box/ig; var str = ' This was a box!,that is a Box too '// Find to return position, otherwise return-1
6. Split
/* split into string arrays using split */ var pattern =//ig; var str = ' This was a box!,that is a Box too '// spaces are split into arrays
Three, several important concepts
1. Greedy mode and non-greedy mode
In the regular expression matching process, the default matches the most fields, for example, with/8.*8/to match ' 8google8 8google8 8google8 ', because. * can match any string, so the default is to remove as close as 8, matching as many features as possible, so the result is ' 8google8 8google8 8google8 ', if you want to meet the first 8 to the cutoff, you need to change to/8.*?8/, that is not greedy, the match result is 8google8.
In design mode, analogy to the A hungry man mode of the singleton mode and lazy mode, a hungry man is very hungry, as much as necessary food, lazy is until the time needed to apply for food.
2. Anchor character
Some special characters in regular expressions, with specific meanings
^: Indicates that this match must start at the beginning of the line
$: The ending of this match must match the end of the line
\b: Match string bounds, the following expression can match ' Google AAA ', but cannot match ' googleaaa '
var str= "Google aaa" ; var pattern=/google\b/ alert (pattern.test (str))
3, the regular expression grouping concept
In a regular expression, you can use () to group, grouping in functions such as Exec,match will return the entire string and grouping of matches, using $ or \1 to get a specified group
4, can take elective omphalos/\w/igm
I ignore case
G Global Match
M multi-line matching
5. Question mark "?" , the question mark is used as a regular expression special character in the following ways:
5.1: Use as a real question mark?
5.2:0 times or 1 times \w? Represents a 0-1-time character
5.3: Non-greedy mode, \w*? Represents a non-greedy match for any number of characters
5.4: Non-capture?:, Goo (?:. *) 8 when capturing using EXEC. * Content is not grouped as a separate group
5.5 means forward-looking capture? = Goo (? =gle)//goo must follow gle to capture
To cite an extreme example
var str= "Gaoo?gle aaa" ; var // There are five question marks in this regular, meaning they are different: 1, the whole group will not be captured, 2, the G after 0 or 1 a,3 for a with a non-greedy match 4, the actual match is represented? Number, 5 means that the entire match fetch must end with GLE, but does not include the GLE // match result: Gaoo?
[Technical Learning]js Regular Expression Summary