Skip to content

Regular expression (regex)

regex is a sequence of characters that specifies a search pattern in text

Syntax

Identifiers:

  • .: any character, except for a newline

  • \d: any digit (0-9)

  • \D: not a digit

  • \w: any character (a-z, A-Z, 0-9, _)

  • \W: not a character

  • \s: whitespace (space, tab, newline)

  • \S: not whitespace

  • \b: the whitespace around words (word boundary)

  • \B: not a word boundary

Modifiers:

  • +: match 1 or more

  • ?: match 0 or 1

  • *: match 0 or more

  • ^: match the beginning of a string

  • $: match the end of the string

  • |: either or

  • [ ]: range or variance [A-Z] (match characters in brackets)

  • [^ ]: not match the range or variance [A-Z]

  • {x}: expecting x amount

  • {1,3}: we're expecting 1 to 3

White Space Characters:

  • \n: new line
  • \s: space
  • \t: tab
  • \e: escape
  • \f: form feed
  • \r: return

DON't FORGET!: { } \

syntax characters:

  • \:
  • .:
  • *:
  • +:
  • ?:
  • (:
  • ):
  • [:
  • ]:
  • {:
  • }:

Groups & Look-around

  • (abc): capture group
  • \1: back-reference to group #1
  • (?:abc): non-capturing group
  • (?=abc): positive lookahead
  • (?!abc): negative lookahead

Quantifiers

  • ?: match never or 1

  • *: match 0 or more times

  • +: match 1 or more times

  • {n}: match n times

  • {n,}: match n or more times

  • {n,m}: match at least n times, at most m times

  • |: match this or that

Atoms

Atoms are the basic building blocks of regular expressions

Lookahead

JavaScript

javascript
const regTerm = /hello/;

console.log(regTerm);
console.log(regTerm.source); // hello

Flags

Literal flagProperty nameESDescription
dhasIndicesES2022Switch on match indices
gglobalES3Match multiple times
iignoreCaseES3Match case-insensitively
mmultilineES3^ and $ match per line
sdotAllES2018Dot matches line terminators
uunicodeES6Unicode mode (recommended)
ystickyES6No characters between matches

Methods

  • exec(): executes a search for a match in a specified string and returns a result array, or null

    javascript
    const regex1 = RegExp("foo*", "g");
    const str1 = "table football, foosball";
    let array1;
    
    while ((array1 = regex1.exec(str1)) !== null) {
      console.log(`Found ${array1[0]}. Next starts at ${regex1.lastIndex}.`);
      // expected output: "Found foo. Next starts at 9."
      // expected output: "Found foo. Next starts at 19."
    }
  • test(): executes a search for a match between a regular expression and a specified string. Returns true or false

    javascript
    const str = "table football";
    
    const regex = new RegExp("foo*");
    const globalRegex = new RegExp("foo*", "g");
    
    console.log(regex.test(str));
    // expected output: true