Regular Expressions in 1 Minute
Tags: regex, javascript
Published: 2020-07-31 07:02:54

## Syntax

**Sets the regex query for "hello":**

```js
re = /hello/
```

**Sets the regex query to be case-insensitive:**

```js
re = /hello/i
```

**Sets the regex query as a global search, searching for all instances, not just the first:**

```js
re = /hello/g
```

<!--
## Evaluations

```js
exec()
```

```js
test()
```

```js
match()
```

```js
search()
```

```js
replace()
```
-->

## Special Characters

### Literal Characters

**Matches with any string containing _exactly_ `hello`, and is _case-sensitive_:**

```js
re = /hello/
```

**Matches with any string containing `hello`, and is _case-insensitive_:**

```js
re = /hello/i
```

### Meta-character Symbols

#### `^` "Must start with"

**Matches with "Hello World":**

```js
re = /^h/i
```

**Matches with "Hello World":**

```js
re = /^hel/i
```

#### `$` "Must end with"

**Matches with "Hello World":**

```js
re = /d$/i
```

**Matches with "Hello World":**

```js
re = / world$/i
```

#### `^...$` "Must begin with and end with"

**Not a match with "Hello World", only Matches "Hello":**

```js
re = /^hello$/i
```

#### `.` "Matches any ONE character"

> Basically a wild card but for ONLY ONE character, try changing `str` to "Hbllo World"

**Matches with "Hello" or "Hbllo" or "Hwllo" or "H7llo" or "H@llo", etc.:**

```js
re = /h.llo/i
```

#### `*` "Matches any character 0 or more times"

> This is a true wild card (also works for zero characters!)"

**Matches with "hello" or "heeeello" or "heebbllo" or "h52340978562llo" or "H!@#\$&^!%#\*@%!%llo", etc.:**

```js
re = /h\*llo/i
```

#### `?` "Optional character"

**Matches with "hello" or "hallo" or "hullo" or "hllo":**

```js
re = /ha?e?u?llo/i
```

#### `\` "Escape character"

**Not a match with "Hello", only Matches "Hello?":**

```js
re = /hello\?/i
```

#### `[ ]` Brackets Character sets

**Matches "hello" or "hallo", but not "hllo" or anything else:**

```js
re = /h[ae]llo/i
```

**Matches "Hello" or "Zello":**

```js
re = /[HZ]ello/i
```

**Not a match; `[^HZ]` = anything EXCEPT H or Z":**

```js
re = /[^HZ]ello/i
```

**Matches ANY uppercase letter:**

```js
re = /[A-Z]ello/
```

**Matches ANY lowercase letter:**

```js
re = /[a-z]ello/
```

**Matches ANY letter with any case:**

```js
re = /[A-Za-z]ello/
```

**Only matches a number like "1ello" or "9ello", NOTE: "1234ello" also matches because it's just looking for a single digit before "ello":**

```js
re = /[0-9]ello/
```

**Only matches a double digit number like "69ello" or "420ello", NOTE: still only looks for 2 digits before "ello":**

```js
re = /[0-9][0-9]ello/
```

#### `{ }` Braces Quantifiers

**Matches if `l` occurs exactly {m} amount of times:**

```js
re = /Hel{2}o/
```

**Matches if `l` occurs 2-4 times:**

```js
re = /Hel{2,4}o/
```

**Matches if `l` occurs at least 2 times:**

```js
re = /Hel{2,}o/
```

#### `( )` Parentheses Grouping

**Matches as long as it finds `Xp` repeating 3 times:**

```js
re = /([0-9]p){3}/
```

**Matches only if it finds `Xp` repeating _exactly_ 3 times:**

```js
re = /^([0-9]){3}$/
```

### Shorthand Character Classes

#### `\w` "Word Character"

**Matches for any letter, number, or underscore `_`, but no other symbols or characters:**

```js
re = /\w/
```

#### `+` "One or More"

**Matches for one or more letter, number, or underscore `_`:**

```js
re = /\w+/
```

#### `\W` "Non-Word Character"

**Matches only if it finds something that's NOT a letter, number, or underscore:**

```js
re = /\W/
```

#### `\d` "Digit Character"

**Matches for a single digit:**

```js
re = /\d/
```

**Matches for one or more digits:**

```js
re = /\d+/
```

#### `\D` "Non-Digit Character"

**Matches for any non-digit character:**

```js
re = /\D/
```

#### `\s` "Whitespace Character"

**Matches for a space, tab, or similar whitespace characters:**

```js
re = /\s/
```

#### `\S` "Non-Whitespace Character"

**Matches for anything other than a space, tab, or similar whitespace characters:**

```js
re = /\S/
```

#### `\b` "Word Boundary"

**Matches for words that have a word boundary at that position, so that you avoid matching strings that contain a word inside of another word, like with Hell & Hello or Beetle & Beetlejuice:**

```js
re = /Hell\b/i
```

### Assertions

#### `x(?=y)` "x followed by y"

**Matches `x` only if followed by `y`:**

```js
re = /x(?=y)/i
```

#### `x(?!y)` "x not followed by y"

**Matches `x` only if NOT followed by `y`:**

```js
re = /x(?!y)/i
```

## Reference: Strings we've used to match in this article

```js
const str = 'Hello World'
const str = '3p3p3p'
const str = 'Hello, welcome to Hell'
const str = 'asgkljhalwxqflife'
```