> For the complete documentation index, see [llms.txt](https://openai.gitbook.io/code-cheatsheets/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://openai.gitbook.io/code-cheatsheets/js/regex.md).

# Regex

See regex under / for regex details

## Create Object

```ts
const re = /pattern/i;
const re = /ab+c/i;
const emailRegex = /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/;

if (emailRegex.test(email)) {}

// Extract parts using capturing groups
const parts = email.match(/^([^@]+)@([^.]+)\.(.+)$/);
if (parts) {
  console.log(`  Username: ${parts[1]}`);
  console.log(`  Domain: ${parts[2]}`);
  console.log(`  TLD: ${parts[3]}`);
}

const highlighted = searchText.replace(
  /javascript/gi,
  (match) => `<mark>${match}</mark>`
);

//Need to escape regex special chars like ., *, +, ?, [, ]
const re = new RegExp(`\\b${searchString}\\b`, "i");
```

## Flags

* By default stops after first match

| Flag | Description                                                                                                                                                                                                  |
| ---- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| `g`  | Global search, meaning does all matches and test maintains lastIndex meaning continuous calls give true the number of matches there are then gives false                                                     |
| i    | Case-insensitive search.                                                                                                                                                                                     |
| m    | Multi-line search.                                                                                                                                                                                           |
| 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. See [`sticky`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/sticky) |

## Functions

Array of info: \["(AERO ST)", index: 18, input: "Aerospace Studies (AERO ST)", groups: undefined];

| Method                                                                                                       | Description                                                                                                                        |                                                                                                                                    |
| ------------------------------------------------------------------------------------------------------------ | ---------------------------------------------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------- |
| [`exec`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/exec)       | `RegExp` method, search for match, return array of info or null                                                                    | var myRe = /d(b+)d/g; var myArray = myRe.exec('cdbbdbsbz');                                                                        |
| [`test`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/test)       | `RegExp` method, check for match, returns true or false                                                                            |                                                                                                                                    |
| [`match`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/match)     | `String` method, search for a match, returns an array of info or null                                                              | "Aerospace Studies (AERO ST)".match(/(.\*)/) => \["(AERO ST)", index: 18, input: "Aerospace Studies (AERO ST)", groups: undefined] |
| [`search`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/search)   | `String` method, that tests for a match in a string. It returns the index of the match, or -1 if the search fails.                 |                                                                                                                                    |
| [`replace`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace) | A `String` method that executes a search for a match in a string, and replaces the matched substring with a replacement substring. |                                                                                                                                    |
| [`split`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/split)     | A `String` method that uses a regular expression or a fixed string to break a string into an array of substrings.                  |                                                                                                                                    |
