# Regular Expression

A regular expression is an object that can either be constructed with the `RegEx` constructor or written as a literal value by enclosing a pattern in a forward slash `(/)` characters. The syntaxes for creating a  regular expression are shown below.&#x20;

```javascript
// using regular expression constructor
new RegExp(pattern[, flags]);

// using literals
/pattern/modifiers
```

The flags are optional while creating a regular expression using literals. Example of creating identical regular using above mentioned method.

```javascript
let re1 = new RegExp("xyz"); 
let re2 = /xyz/;
```

Both ways will create a regex object and have the same methods and properties. There are cases where we might need dynamic values to create a regular expression, in that case, literals won't work and have to go with the constructor. &#x20;

{% hint style="info" %}
In cases where we want to have a forward slash to be a part of a regular expression, we have to escape the forward slash `(/)` with backslash `(\)`.
{% endhint %}

The different modifiers that are used to  perform case-insensitive  searches are:

* `g` -  global search (finds all  matches instead of stopping after the first match)&#x20;
* `i`- case insensitive search
* `m` - multiline matching

*Brackets* are used in a regular expression to find a range of characters. Some of them are mentioned below.

* `[abc]` - find any character between the brackets
* `[^abc]` - find any character, not between the brackets
* `[0-9]` - find any digit between the bracket
* `[^0-9]` - find any character, not between the brackets (non-digit)
* `(x|y)`- find any of the alternatives separated by |

*Metacharacters* are special character that has special meaning in the regular expression. These characters are further described below:

<table><thead><tr><th width="176">Metacharacter</th><th>Description</th></tr></thead><tbody><tr><td><code>.</code></td><td>Match a single character excpet newline or a terminator</td></tr><tr><td><code>\w</code></td><td>Match a word character (alphanumeric character <code>[a-zA-Z0–9_]</code>)</td></tr><tr><td><code>\W</code></td><td>Match a non word character (same as <code>[^a-zA-Z0–9_]</code>)</td></tr><tr><td><code>\d</code></td><td>Match any digit character( same as <code>[0-9]</code>)</td></tr><tr><td><code>\D</code></td><td>Match any non digiti character</td></tr><tr><td><code>\s</code></td><td>Match a whitespace character (spaces, tabs etc)</td></tr><tr><td><code>\S</code></td><td>Match a non whitespace character</td></tr><tr><td><code>\b</code></td><td>Match at the begining / end of a word</td></tr><tr><td><code>\B</code></td><td>Match but not at the begining / end of a word</td></tr><tr><td><code>\0</code></td><td>Match a <code>NULL</code> character</td></tr><tr><td><code>\n</code></td><td>Match a new line character</td></tr><tr><td><code>\f</code></td><td>Match a form feed character</td></tr><tr><td><code>\r</code></td><td>Match a carriage return character</td></tr><tr><td><code>\t</code></td><td>Match a tab character</td></tr><tr><td><code>\v</code></td><td>Match a tab vertical character</td></tr><tr><td><code>\xxx</code></td><td>Match a character specified by an octal number <code>xxx</code></td></tr><tr><td><code>\xdd</code></td><td>Match a character specified by a hexadecimal number <code>dd</code></td></tr><tr><td><code>\udddd</code></td><td>Match Unicode character specified by a hexadecimal number <code>dddd</code></td></tr></tbody></table>

Properties and methods supported by RegEx are listed below.

<table><thead><tr><th width="169">Name</th><th>Description</th></tr></thead><tbody><tr><td><code>constructor</code> </td><td>Returns function that created RegExp  object's protype</td></tr><tr><td><code>global</code></td><td>Checks if the <code>g</code> modifier is set</td></tr><tr><td><code>ignoreCase</code></td><td>Checksi if the <code>i</code> modifier is set</td></tr><tr><td><code>lastIndex</code></td><td>Specifies the index at which to start the next match</td></tr><tr><td><code>multiline</code></td><td>Checksi if the m modifier is set</td></tr><tr><td><code>source</code></td><td>Returns the text of the sttring</td></tr><tr><td><code>exec()</code></td><td>Test for the match and returns the first match, if no match then it returns <code>null</code></td></tr><tr><td><code>test()</code></td><td>Test for the match and returns the <code>true</code> or <code>false</code> </td></tr><tr><td><code>toString()</code></td><td>Returns the string value of the regular exression</td></tr></tbody></table>

{% hint style="warning" %}
`A complie()` method complies the regular expression and is deprecated.&#x20;
{% endhint %}

Some examples of regular expressions are shown here.

```javascript
let text = "The best things in life are free";
let result = /e/.exec(text); // looks for a match  of e in a string
// result: e


let helloWorldText = "Hello world!";
// Look for "Hello"
let pattern1 = /Hello/g;
let result1 = pattern1.test(helloWorldText);
// result1: true

let pattern1String = pattern1.toString();
// pattern1String : '/Hello/g'
```
