Regex 官方文档
Anchors
Anchros
- ^
- Start of string, or start of line in multi-line pattern
- \>
- End of word
- \<
- Start of word
- \A
- Start of string
- \b
- Word boundary
- \Z
- End of string
- $
- End of string, or end of line in multi-line pattern
Character
Character Classes
- \c
- Control character
- \d
- Digit
- \O
- Octal digit
- \s
- White space
- \w
- Word
- \x
- Hexade-cimal digit
Special
- \f
- Form feed
- \n
- New line
- \r
- Carriage return
- \t
- Tab
- \v
- Vertical tab
- \xhh
- Hex character hh
- \xxx
- Octal character xxx
Examples
Metacharacter
- ^abc
- abc, abcdefg, abc123, ...
- (abc){2}
- abcabc
- a.c
- abc, aac, acc, adc, aec, ...
- a[bB]c
- abc, aBc
- a\sc
- a c
- ab?c
- ac, abc
- ab{2}c
- abbc
- ab*c
- ac, abc, abbc, abbbc, ...
- ab+c
- abc, abbc, abbbc, ...
- abc$
- abc, endsinabc, 123abc, ...
- bill|ted
- ted, bill
Groups
Groups and Ranges
- .
- Any character except new line (\n)
- (?:...)
- Passive (non-c-apt-uring) group
- (...)
- Group
- (a|b)
- a or b
- [^abc]
- Not a or b or c
- [0-7]
- Digit from 0 to 7
- [a-q]
- Letter from a to q
- [abc]
- Range (a or b or c)
Modifiers
Modifiers
- e
- Evaluate replac-ement
- g
- Global match
- i
- Case-i-nse-nsitive
- m
- Multiple lines
- s
- Treat string as single line
- U
- Ungreedy pattern
- x
- Allow comments and white space in pattern
String
Replacement
- $`
- Before matched string
- $'
- After matched string
- $&
- Entire matched string
- $+
- Last matched string
- $1
- "-xyz-" in /^(?:a-bc)-(xyz)$/
- $2
- "-xyz-" in /^(abc-(xy-z))$/
- $n
- nth non-pa-ssive group
Assertions
Assertions
- ?!
- Negative lookahead
- ?!= or ?<!
- Negative lookbehind
- ?()
- Condition [if then]
- ?()|
- Condition [if then else]
- ?>
- Once-only Subexp-ression
- ?<=
- Lookbehind assertion
- ?#
- Comment
- ?=
- Lookahead assertion