# Regular Expressions (Regex) in JavaScript

Regular Expressions (Regex) help us **search, match, and manipulate text patterns** inside strings.

## 1\. Basic Format of Regex

```javascript
/pattern/flags
```

Example:

```javascript
/hello/g
```

*   `/` → Start and end of regex
    
*   `hello` → pattern
    
*   `g` → flag
    

* * *

## 2\. Flags

<table style="min-width: 50px;"><colgroup><col style="min-width: 25px;"><col style="min-width: 25px;"></colgroup><tbody><tr><th colspan="1" rowspan="1"><p>Flag</p></th><th colspan="1" rowspan="1"><p>Meaning</p></th></tr><tr><td colspan="1" rowspan="1"><p><code>g</code></p></td><td colspan="1" rowspan="1"><p>Global → match all occurrences</p></td></tr><tr><td colspan="1" rowspan="1"><p><code>i</code></p></td><td colspan="1" rowspan="1"><p>Case insensitive</p></td></tr><tr><td colspan="1" rowspan="1"><p><code>m</code></p></td><td colspan="1" rowspan="1"><p>Multi-line</p></td></tr></tbody></table>

### `g` → Global (match all occurrences)

```javascript
const str = "cat cat dog";
console.log(str.match(/cat/g));
```

**Output:**

```javascript
["cat", "cat"]
```

Without `g`, it would return only the first match.

### `i` → Case insensitive

```javascript
const str = "Cat cat CAT";
console.log(str.match(/cat/gi));
```

**Output:**

```javascript
["Cat", "cat", "CAT"]
```

* * *

### 3\. `+` → One or More

`/e+/g` → Match at least one `e`

```javascript
const str = "heeeello";
console.log(str.match(/e+/g));
```

**Output:**

```javascript
["eeee"]
```

Explanation:

*   `+` means one or more times.
    

* * *

### 4\. `?` → Optional (0 or 1 time)

## `/e+a?/g`

```javascript
const str = "eeea eee";
console.log(str.match(/e+a?/g));
```

**Output:**

```javascript
["eeea", "eee"]
```

Explanation:

*   `e+` → one or more e
    
*   `a?` → a is optional
    

* * *

### 5\. `*` → Zero or More

## `/ea*/`

```javascript
const str = "e ea eaaa";
console.log(str.match(/ea*/g));
```

**Output:**

```javascript
["e", "ea", "eaaa"]
```

Explanation:

*   `a*` → zero or more a
    

* * *

### 6\. `.` → Any Single Character

## `/.at/`

```javascript
const str = "cat fat hat at";
console.log(str.match(/.at/g));
```

**Output:**

```javascript
["cat", "fat", "hat"]
```

Explanation:

*   `.` matches any one character before `at`.
    

* * *

### 7\. Escape Character `\`

## `/.\./`

```javascript
const str = "a.b c.d";
console.log(str.match(/.\./g));
```

**Output:**

```javascript
["a.", "c."]
```

Explanation:

*   `\.` means match actual dot (`.`).
    

* * *

### 8\. `\w` → Word Character

Matches:

*   a-z
    
*   A-Z
    
*   0-9
    
*   underscore `_`
    

```javascript
const str = "Hello_123!";
console.log(str.match(/\w/g));
```

**Output:**

```javascript
["H","e","l","l","o","_","1","2","3"]
```

### 9\. `\s` → Whitespace

```javascript
const str = "Hello World";
console.log(str.match(/\s/g));
```

**Output:**

```plaintext
[" "]
```

* * *

### 10\. `{min,max}` → Quantifiers

## `/\w{1,3}/g`

```plaintext
const str = "JavaScript";
console.log(str.match(/\w{1,3}/g));
```

**Output:**

```plaintext
["Jav", "aSc", "rip", "t"]
```

Explanation:

*   Match minimum 1 and maximum 3 characters.
    

* * *

### 11\. Character Sets `[]`

## `/[fc]at/`

```plaintext
const str = "fat cat bat";
console.log(str.match(/[fc]at/g));
```

**Output:**

```plaintext
["fat", "cat"]
```

Explanation:

*   Match either `f` or `c` before `at`.
    

### 12\. `[a-z]` → Character Range

Explanation: Match any character between a and z.

```plaintext
const str = "ABCdef";
console.log(str.match(/[a-z]/g));
```

**Output:**

```plaintext
["d", "e", "f"]
```

### 13\. `(a|b)` → OR Operator

Explanation: Match either `a` or `b`.

```plaintext
const str = "Tiger tiger";
console.log(str.match(/(T|t)/g));
```

**Output:**

```plaintext
["T", "t"]
```

Better way:

```plaintext
console.log(str.match(/t/gi));
```

* * *

### 14\. `^` → Start of Line

Explanation: Match only if string starts with pattern.

```plaintext
console.log(/^Hello/.test("Hello World"));
```

**Output:**

```plaintext
true
```
