Regex

See regex under / for regex details

Create Object

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 stickyarrow-up-right

Functions

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

Method
Description

RegExp method, search for match, return array of info or null

var myRe = /d(b+)d/g; var myArray = myRe.exec('cdbbdbsbz');

RegExp method, check for match, returns true or false

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]

String method, that tests for a match in a string. It returns the index of the match, or -1 if the search fails.

A String method that executes a search for a match in a string, and replaces the matched substring with a replacement substring.

A String method that uses a regular expression or a fixed string to break a string into an array of substrings.

Last updated