Cheat Sheet
Character | Meaning | Example |
---|---|---|
^ | the beginning of the string | |
$ | the end of the string | |
\b | whole word only | \babc\b matches 'abc', but not 'abcc ' |
\B | if the pattern is fully surrounded by word | \Babc\B mathes 'aabcc', but not 'abc' |
. | anything | |
\d | single digit in 0-9 | |
\D | single non-digit | |
\w | single word character | |
\W | single non-word | |
\s | white space(space, tab, return, new line) | |
\S | non-whitespace | |
\t | tab | |
\r | return | |
\n | new line | |
\g | global search, which means it doesn't stop at the first match | |
* | zero or more of the previous | abc* matches a string that has ab followed by zero or more c |
+ | one or more of the previous | abc+ matches a string that has ab followed by one or more c |
? | zero or one of the previous | abc? matches a string that has ab followed by zero or one c |
?: | non-capturing group | |
X{m} | number of X === m | |
X{m,} | m < number of X | |
X{m, n} | m < number of X < n | |
(X | Y) | X or Y | |
[...] | any of characters in the class | [abcd] matches a string that has one of the char (a, b, c, d) |
- | range | [a-d] same as above |
Key words from the cheat sheet
Anchor
Anchor is not a pattern, it is a position.
It matches a position before, after or between characters.
For example, ^
is the beginning of the string, and it is also a start of line anchor.
Boundaries\b
is a word boundary because \babc\b
has to match the whole word abc
.\B
is non-word-boundary because \Babc\B
means any words that includes abc
(abcc, eabc).
QuantifiersGreedy
: The optional item is included in the match if possible. ?
is a typical example of greedy quantifiers. abc?
can match either abc
or ab
.Lazy
: * The optional item is excluded in the match if possible. ??
is a typical one because abc??
only matches ab
.
Non-capturing group?:
means the pattern doesn't capture to the group. It usually uses with .match()
. So if you use ?:
, then the pattern won't be included to the array.
Comments
Post a Comment