-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add regular expressions cheat sheet
- Loading branch information
Showing
1 changed file
with
68 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
# Regular Expressions | ||
|
||
## Basic Example | ||
|
||
```javascript | ||
const regex = /[A-Z]/g | ||
|
||
const anotherRegex = new RegExp(/[A-Z]/, 'g') | ||
|
||
const yetAnotherRegex = new RegExp('[A-Z]', 'g') | ||
|
||
anotherRegex.test('Apple') // true | ||
anotherRegex.test('apple') // false | ||
|
||
``` | ||
|
||
## Cheat Sheet | ||
|
||
Source: [RegExr](https://regexr.com/) | ||
|
||
### Character classes | ||
|
||
| Symbol | Meaning | | ||
| --- | --- | | ||
| `.` | any character except newline | | ||
| `\w\d\s` | word, digit, whitespace | | ||
| `\W\D\S` | not word, digit, whitespace | | ||
| `[abc]` | any of `a`, `b`, or `c` | | ||
| `[^abc]` | not `a`, `b`, or `c` | | ||
| `[a-g]` | character between `a` & `g` | | ||
|
||
### Anchors | ||
|
||
| Symbol | Meaning | | ||
| --- | --- | | ||
| `^abc$` | start / end of the string | | ||
| `\b\B` | word, not-word boundary | | ||
|
||
### Escaped characters | ||
|
||
| Symbol | Meaning | | ||
| --- | --- | | ||
| `\.\*\\` | escaped special characters | | ||
| `\t\n\r` | tab, linefeed, carriage return | | ||
|
||
### Groups & Lookaround | ||
|
||
| Symbol | Meaning | | ||
| --- | --- | | ||
| `(abc)` | capture group | | ||
| `\1` | backreference to group #1 | | ||
| `(?:abc)` | non-capturing group | | ||
| `(?=abc)` | positive lookahead | | ||
| `(?!abc)` | negative lookahead | | ||
|
||
### Quantifiers & Alternation | ||
|
||
| Symbol | Meaning | | ||
| --- | --- | | ||
| `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 | | ||
|
||
## Resources | ||
|
||
* [RegExr](https://regexr.com/): An online tool that lets you test a regex against different strings. You can see where the matches are, and it breaks down what each part of the regex is looking for. |