https://github.com/utkuufuk/regexref
Regex reference sheet
https://github.com/utkuufuk/regexref
Last synced: 7 months ago
JSON representation
Regex reference sheet
- Host: GitHub
- URL: https://github.com/utkuufuk/regexref
- Owner: utkuufuk
- Created: 2019-04-29T04:55:03.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2019-04-29T08:25:56.000Z (over 6 years ago)
- Last Synced: 2025-01-29T07:48:41.518Z (8 months ago)
- Size: 1.95 KB
- Stars: 2
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# RegEx Reference
1. [Tools](#tools)
2. [Cheatsheet](#cheatsheet)
3. [Examples](#examples)
4. [Programming Languages](#programming-languages)## Tools
1. [Online Tool](https://regex101.com/)2. ```sh
grep -P "" # omit leading & trailing '/' from regex
```
3. ```sh
sag "" # omit leading & trailing '/' from regex
```
4. VS Code: `Ctrl+F > Alt+R`## Cheatsheet
### Common
| RegEx | Description |
|:------|:--------------------------------------|
| . | Any Character Except New Line |
| \d | Digit (0-9) |
| \D | Not a Digit (0-9) |
| \w | Word Character (a-z, A-Z, 0-9, _) |
| \W | Not a Word Character |
| \s | Whitespace (space, tab, newline) |
| \S | Not Whitespace (space, tab, newline) |
| \b | Word Boundary |
| \B | Not a Word Boundary |
| ^ | Beginning of a String |
| $ | End of a String |
| [] | Matches Characters in brackets |
| [^ ] | Matches Characters NOT in brackets |
| ( ) | Either Or Group |### Quantifiers
| RegEx | Description |
|:-------|:------------------------------------|
| * | 0 or More |
| + | 1 or More |
| ? | 0 or One |
| {3} | Exact Number |
| {3,4} | Range of Numbers (Minimum, Maximum) |## Examples
### Exact Match
```sh
ninja # 'ninja'
^ninja$ # exactly 'ninja' and nothing else
```### Character Set
``` sh
[ng]inja # 'ninja' or 'ginja'
ninja[1-9] # 'ninja1' to 'ninja9'
[^n]inja # every '_inja' except 'ninja'
```### Multiple Characters
``` sh
[0-9]+ # multiple digits
a{3} # 3 'a's
[a-zA-Z]{5,8} # 5 to 8 characters
```### Optional/Alternate Matches
``` sh
(cat|dog|bird) # 'cat' or 'dog' or 'bird'
M(r|s|rs)\.? # 'Mr/Mr./Ms/Ms./Mrs/Mrs.'
```## Programming Languages
### JavaScript
``` javascript
// these two are equivalent
const reg1 = /[a-z]/i
const reg2 = new RegExp(/[a-z]/, 'i')reg1.test("b") // true
reg2.test("3") // false
```#### Matching Groups
``` javascript
const text = "http://www.google.com";
const regex = /https?://(www\.)?(\w+)(\.\w+)/
const [whole, www, domain, extension] = text.match(regex)
```