Intro to Regular Expressions

RegEx for Beginners in JavaScript

What is a regular expression?

Popular Regex and String Methods


const string = "This is an example of test"
const regex = /exam/;
console.log(regex.test(string)) =>> true


const string = "This is an example of the match method";
var regex =/ match/
console.log(str.match(regex)); ==> [ "match" ]

To find all matches, we use the g (global) flag(see below)

const string = "This is a test to test match method";
const regex =/test/g
console.log(str.match(regex)); ==>[ "test", "test" ]

In case of no matches, null is returned (and NOT an empty array. Important to remember while applying array methods).

var string = "This is a test" ;
console.log(str.match(/hello/)); ==> null


  • i: Ignores casing (/e/i will match both ‘e’ and ‘E’)
  • g: Global search returning all matches for a given expression inside a string — without it, only the first match is returned

These flags can be used separately or together in any order as seen down below.

conts string = "Hello, hello";
==> Array(2)[ "He","he"]

Groups and ranges

For example [bd] will match for ‘b’ or ‘d’

console.log(/[bd]/.test("bed")); =>>true
console.log(/[bd]/.test("medical")); ==>true
console.log(/[bd]/.test("zoo")); ==>false

Character classes

\d = Any digit character (from 0 to 9)

\D = Non-digit: any character except \d

\w = Any alphanumeric character from the basic Latin alphabet (including. digit), including the underscore

\W = Non-wordly character: anything but \w.

\s = a single white space character, including space, tab, form feed, line feed, and other Unicode spaces

\S = Non-space: any character except \s, for instance a letter

Full-Stack Software Engineer / New York City /

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store