Escaped characters
Metacharacter | Description |
---|---|
\. \* \\ | The backslash is used to escape a special character |
Control characters
Metacharacter | Description |
---|---|
\t | Find a tab character |
\v | Find a vertical tab character |
\n | Find a new line character |
\r | Find a carriage return character |
Groups & Lookaround
Metacharacter | Description |
---|---|
[abc] | any of a, b, or c |
[^abc] | not a, b, or c |
[a-g] | the character between a & g |
[A-Z] | Any character from uppercase A to uppercase Z |
[a-z] | Any character from lowercase a to lowercase z |
[A-z] | Any character from uppercase A to lowercase z |
[0-9] | any numbers or span of numbers from 0 to 9 |
(abc) | capture group |
\1 | a backreference to group #1 |
(?:abc) | non-capturing group |
(?=abc) | positive lookahead |
(?!abc) | negative lookahead |
Opening and closing square bracket matches a range of characters
Opening and closing parenthesis used to group characters
A(?=B) → look for A, but match only if followed by B.
A(?!B) → look for A, but match only if not followed by B.
let text = `I have 2 notes of 100₹`
let pattern = /\d+(?=₹)/gm;
text.match(pattern)
// Output
['100']
Here 2 is ignored and 100 is matched because it is followed by ₹
let text = `I have 2 notes of 100₹`
let pattern = /\d+\b(?!₹)/gm;
text.match(pattern)
// Output
['2']
Here 100 is ignored and 2 is matched because it is not followed by ₹
Some more to read
Metacharacter | Description |
---|---|
\B | Find a match, but not at the beginning/end of a word |
\0 | Find a NULL character |
\f | Find a form feed character |
\xxx | Find the character specified by an octal number xxx |
\xdd | Find the character specified by a hexadecimal number dd |
\udddd | Find the Unicode character specified by a hexadecimal number dddd. For example \u00A9 → Unicode escaped © |
Quantifiers & Alternation
Metacharacter | Description |
---|---|
^ | matches any string at the beginning of it. for example ^i Search for i at the beginning |
a* a+ a? | 0 or more, 1 or more, 0 or 1 |
a{5} a{2,} | exactly five, two or more |
a{1,3} | between one & three |
a+? a{2,}? | match as few as possible |
ab|cd | match ab or cd |
------------------------- Click here to continue -------------------------