JavaScript regular expression definition (syntax)
2 ways to define regular expressions: One is to call RegExp directly (), and the second is to define it directly with the literal, i.e. var re =/regular rule/;
2 defining methods are essentially calling the RegExp () method
ECMASCRIPT3 and ECMAScript5 behave differently when calling the same regular code
?
functionreg(){ var re = /\sjavascript/; returnre; } |
Call the Reg () method multiple times in ECMASCRIPT3 and ECMAScript5, respectively
In ECMAScript3, the same RegExp object is called, and in ECMAScript5, a different RegExp object is called because a new EXCMAScript5 object is generated each time it is executed in regexp
Therefore, the hidden danger of the program is caused in the ECMASCRIPT3, because whenever the object is modified in one place, all calls to this object will change.
1. Direct Volume characters
Characters are usually directly matched in regular, such as
?
Will directly match the character JavaScript
Non-alphabetic character matching is also supported, such as:
\o nul character (\u0000)
\ t Tab (\u0009)
\ n line break (\u000a)
\v vertical tab (\U000B)
\f page Break (\u000c)
\ r return character (\u000d)
\xnn the Latin character specified by the hexadecimal number NN, for example, \x0a equivalent to \ n
\uxxxx The Unicode character specified by the hexadecimal number XXXX, for example \u0009 equivalent to \ t
\CX control character ^x, for example, \CJ equivalent to line break \ n
In regular expressions, there are some punctuation marks that have special meanings, and they need ' \ ' to escape
^$.*+?=!:| \/()[]{}
2. Character classes
[...] Any character in square brackets
[^...] Any character that is not in square brackets
. Any character
\w A word that consists of any ASCII character, equivalent to [a-za-z0-9]
\w any word that is not an ASCII character, equivalent to [^a-za-z0-9]
\s any Unicode whitespace character
\s any non-Unicode whitespace characters, note that \w and \s are not the same
\d any ASCII value equivalent to [0-9]
\d any character except ASCII digits, equivalent to [^0-9]
[\b] backspace direct volume (special case)
3. Repetition (number of times)
? 0 or 1 times
+ 1 or more times
* Any time
{n} n times
{m,n} at least m times, up to N times
{n,} n or N times
The regular default is greedy match.
such as [a+b+] if you want to match Aaabb, it will not match AB and AAB, etc., will only match Aaabb
[a+?b+?] This will match Aaab why does it make this difference?
A: +? is to let the regular non-greedy match, then B here will only match a B, then why a will match 3? This is because pattern matching of regular expressions always looks for the first possible match in a string.
4. Options | group | reference
| For separating the characters that are available for selection, such as [AB|CD], he can either match ab or match the CD, note: The attempt to match the selection of items is left → right, so [A|ab], when a match passes, it does not match AB, even if AB is better matched
() 1. Individual items as sub-expressions/java (script)?/can match JavaScript and Java, which is the parenthesis part of the expression, can be executed on a subexpression | * Wait for the operation.
2. The complete pattern defines a sub-pattern that can refer to an expression preceded by parentheses/([' "]) [a-z]\1/\1 refers to the expression in the first parenthesis and therefore references ['"]
3. The latter refers to the preceding subexpression note:/[' "][a-z]['"]/this regular means single or double quotes plus a lowercase letter plus a single or double quotation marks, and the single and double quotes are not matched
If you want to match this can be written [[' "]] [a-z]\1]
\ Plus number can refer to an expression in the preceding parentheses
5. Make a matching position (anchor point)
^ matches the beginning of a string, in a multi-row search, matches the beginning of a line
$ matches the end of a string, in multiple-row retrieval, matches the end of a line
\b matches the boundary of a word, in short, the position between the character \w and \w, or the position between the character \w and the beginning or end of the string.
\b matches the position of a non-word boundary
(? =p) 0 Wide forward assertion that requires the next character to match p, but cannot include those characters that match P
(?! P) 0 Wide negative lookahead assertion, requires that the next character does not match p
6. Modifiers
Written on the regular expression literal//right
I perform a case-insensitive match
G performs a global match, in short, finds all matches instead of stopping after the first one is found
M multi-line matching pattern, ^ matches the beginning of a line and the beginning of a string, the end of the matching line and the end of the string/java$/m can match Java\nfunc
Note: When the regular expression is global, each time the exec () and test () is set to the current lastindex position, the execution will start from the lastindex position, so it is best to set the lastindex to 0 per execution.
Article from Leo Column
JavaScript regular expression definition (syntax)