JavaScript by Example: Regular Expressions

Regular expressions (commonly referrex to as RegEx) in JavaScript help performing a few action against strings.

RegEx are used for:

  • Searching text
  • Replacing text
  • Validating text
// Searching text
let text = "JavaScript by Example";
let n = text.search(/JavaScript/);

// Matching text
let text = "JavaScript by Example";
let result = text.replace(/JavaScript/i, "JS");

// Replacing text
let text = "JavaScript by Example";
let n = text.match(/JavaScript/);