Learning the Basics of RegEx

A quick primer on what RegEx is and how you can use it in your JavaScript projects

What Are Regular Expressions?

What Does A Regular Expression Look Like And How Are They Used?

// LITERAL EXPRESSION
let re = /ab+c/;
// USING REGEXP OBJECT
let re = new RegExp('/ab+c/')
// LITERAL EXPRESSION 
let re = /ab+c/;
// INFORMATION WE WANT TO PARSE
let example = "Hi, do you know your abc's?";
// USING THE REGULAR EXPRESSION TO FIND SUBSTRINGS THAT MATCH OUR CRITERIA
example.match(re);
// => ["abc"]
// LITERAL EXPRESSION
let re = /success/;
let example = "Error: Submission Unsuccessful";example.match(re);
//=> ["success"]

Characters Classes, Quantifiers, Groups, and Ranges

// Alphanumeric characters && Non-alphanumeric characters
let re = /\w/ && let re = /\W/
// Digits && Non-digits
let re = /\d/ && let re = /\D/
//Whitespace && Non-whitespace
let re = /\s/ && let re = /\S/
// 0 or More (Kleen Star)
let re = /a*/;
let example = "hello";example.match(re);
//=> [""]
//1 or More (Kleen Plus)
let re = /a+/;
let example = "hello";example.match(re);
//=> null
//Optional character
let re = /a?/;
let example = "hello";example.match(re)
// => [""]
// Repetitions
let re = /a{1,3}/;
let example = "apple"example.match(re)
//=> ["a"]
let re = /[dl]og/;let example = "dog" OR "bog" OR "log"example.match(re)
//=> ["dog"] OR null OR ["log"]
// ANOTER EXAMPLE WOULD BE
let re = /[A-Z]/;
example = "here's An example"example.match(re)
//=> ["A"]
let re = /(\w+ \d{2}) \d{4}/;let birthday = "January 07 1993";birthday.match(re)
//=> ["January 07 1993", "January 07"]

Music Fanatic, Software Engineer, and Cheeseburger Enthusiast. I enjoy writing about music I like, programming, and other things of interest.

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