Prakad Alpha Blog

Regex Basics Javascript Cheatsheet

May 09, 2020

Introduction

  • Regex are the most commonly used ways for pattern matching, searching and replacing
  • Below are some of the most commonly used regex concepts explained in simplest way possible.
  • For more advanced expressions and explanation below are some links that might help.

Creating a REGEX expression:

  • Regex Constructor

    let regex = new RegExp(pattern[, flags]);

  • Regex Literal ➡ Starts and Ends with /

    let ex = /hello/

Flags:

  • i ➡ Case insensitive

  • g ➡ Global matching

  • m ➡ Multi-line search.

  • s ➡ Allows . to match newline characters.

  • u ➡ Unicode

  • y ➡ Sticky search that matches starting at the current position in the target string.(uses lastIndex)

    Methods:

  • ex.exec('hellowolrd') ➡ Returns the index in array or null

    const myRe = /(?!a)\w/g;
    let myArray;
    while( (myArray = myRe.exec('appf apsnds aabb')) !== null){
      console.log(myArray);
    }
    
    //Output
    [ 'dbbd', index: 1, input: 'cdbbddbbdsbz', groups: undefined ]
    [ 'dbbd', index: 5, input: 'cdbbddbbdsbz', groups: undefined ]
  • ex.test('phrase') ➡ Tests and returns true or false

  • phrase.match(ex) ➡ Same like exec but returns the matches in array without looping for g flags.

    const myRe = /db+d/g;
    let myArray
    myArray = 'cdbbddbbdsbz'.match(myRe) 
    console.log(myArray)
    //Output
    [ 'dbbd', 'dbbd' ]
  • phrase.search(ex) ➡ Returns the index of the match else -1

  • phrase.replace(ex, 'newPhrase') ➡ Returns string with replaced value

    Symbols:

  • ^ ➡ Anything that starts with => /^h/

  • $ ➡ anything that ends with => /world$/

  • . ➡ matches any one char

  • * ➡ matches any 0 or more times

  • ? ➡ optional char => /gre?a?y/ => e or a or none

  • + ➡ one or more

    Brackets::

  • [] ➡ used for char set

    /gr[ea]y/ => must be a or e

  • [^] ➡ Used for exception

    /[^gf]ray/ => Must not be g or f

  • [a-zA-Z0-9] ➡ Used for range

    /[A-Z]ray/ => anything in that range

  • {} ➡ Quantifiers, Used to restrict the number of elements

     `/re{2}g/` ➡ 2 e's are allowed  
     `/re{2,4}g/` ➡ 2 to 4 e's are allowed   
     `/re{2,}g/` ➡ atleast 2 times e
  • () ➡ Used for grouping, also $1$2 (while replacing) or \1\2 can be used for specifying the groups in same statement

    Shorthands:

  • \w ➡ word char or _(underscore)

  • \W ➡ non-word char

  • \d ➡ any digit

  • \D ➡ any non-digit

  • \s ➡ any space

  • \S ➡ any non space

  • \b ➡ word boundary(only that word, matches the zero-width position)

    Lookaheads:

    Positive - /s(?=y)/ ➡ s followed by y
    Negative - /s(?!y)/ ➡ s not followed by y

References:

Platforms for Practice:


Written by Prakadheshwaran Follow me on Twitter