# JavaScript Regular Expressions

Regular expressions are patterns used to match character combinations in strings. In JavaScript, regular expressions are also objects.

## 1. Creating a Regular Expression

You can construct a regular expression in two ways:

### Using a Regular Expression Literal
This consists of a pattern enclosed between slashes.

```javascript
const re = /ab+c/;
```

### Calling the Constructor Function
This is useful when the pattern is dynamic (e.g., coming from user input).

```javascript
const re = new RegExp("ab+c");
```

## 2. Flags

Flags are optional parameters that affect the search.

*   `g`: Global search.
*   `i`: Case-insensitive search.
*   `m`: Multi-line search.
*   `s`: Allows `.` to match newline characters.
*   `u`: "Unicode"; treat a pattern as a sequence of Unicode code points.
*   `y`: Perform a "sticky" search that matches starting at the current position in the target string.

```javascript
const re = /hello/gi; // Global and Case-insensitive
```

## 3. RegExp Methods

### `test()`
Returns `true` or `false` if the pattern matches the string.

```javascript
const re = /hello/i;
console.log(re.test("Hello World")); // true
```

### `exec()`
Returns an array containing the matched text if a match is found, or `null` otherwise. It is useful for capturing groups or iterating over multiple matches with the `g` flag.

```javascript
const re = /quick\s(brown).+?(jumps)/ig;
const result = re.exec("The Quick Brown Fox Jumps Over The Lazy Dog");

console.log(result[0]); // "Quick Brown Fox Jumps" (Full match)
console.log(result[1]); // "Brown" (Capture group 1)
console.log(result[2]); // "Jumps" (Capture group 2)
```

## 4. String Methods with Regex

Strings have several methods that accept regular expressions.

### `match()`
Retrieves the result of matching a string against a regular expression.

```javascript
const str = "The rain in SPAIN stays mainly in the plain";
const result = str.match(/ain/gi);
console.log(result); // ["ain", "AIN", "ain", "ain"]
```

### `matchAll()`
Returns an iterator of all results matching a string against a regular expression, including capturing groups.

```javascript
const regexp = /t(e)(st(\d?))/g;
const str = 'test1test2';
const array = [...str.matchAll(regexp)];

console.log(array[0]); // ['test1', 'e', 'st1', '1', index: 0, input: 'test1test2', groups: undefined]
console.log(array[1]); // ['test2', 'e', 'st2', '2', index: 5, input: 'test1test2', groups: undefined]
```

### `replace()`
Searches for a match and replaces it with a replacement string.

```javascript
const p = 'The quick brown fox jumps over the lazy dog. If the dog reacted, was it really lazy?';
const regex = /dog/gi;

console.log(p.replace(regex, 'ferret'));
// "The quick brown fox jumps over the lazy ferret. If the ferret reacted, was it really lazy?"
```

### `search()`
Tests for a match in a string. It returns the index of the match, or -1 if the search fails.

```javascript
const str = "hey JudE";
const re = /[A-Z]/g;
console.log(str.search(re)); // 4 (index of 'J')
```

### `split()`
Uses a regular expression or a fixed string to break a string into an array of substrings.

```javascript
const str = "Hello World. How are you doing?";
const words = str.split(/\s+/); // Split by whitespace
console.log(words); // ["Hello", "World.", "How", "are", "you", "doing?"]
```

[[programming/javascript/vanilla/javascript]]
[[programming/regular-expressions]]