Skip to main content
Web Dev

My Regex Reference

This is just a reference I can easily find to deal with regular expressions in Javascript.

I will iterate on this at some point in the future. For now, it’s just a quick reference for me to find certain regex patterns that I use at work.

.test()

regexExpression.test(valueToTest);

Test Exact String

let myString = "I ate the apple";

let the = /the/;

let THE = /THE/;

console.log(the.test(myString)); // true

console.log(THE.test(myString)); // false

Test Multiple strings

let multiString = /ate|apple/;

let multiStringFail = /eated|some|oranges/;

// console.log(multiString.test(myString)); // true

// console.log(multiStringFail.test(myString)); // false

Test & Ignore Capitalization

// uses the i flag

let capitalization = /ATe/i;

// console.log(capitalization.test(myString)); // true
.match()

valueToTest.match(regexExpression);

Extract Exact Matches

let myNewString = "Can extract a word from this text?";

let stringToExtract = /extract/;

// console.log(myNewString.match(stringToExtract)); // \['extract']

Find Multiple Matches

let repeatedString = "Don't ever ever ever do that!";

let repeatMatch = /ever/g;

// console.log(repeatedString.match(repeatMatch)); //\[ 'ever', 'ever', 'ever' ]

Wildcard Multiple Match

let wildcard = "It's fun in the sun but don't burn your bun, hun!"

let wildcardMatch = /.un/g;

// console.log(wildcard.match(wildcardMatch)); //\[ 'fun', 'sun', 'bun', 'hun' ]

Single Character, Multiple Possibilities Match

let possibilities = "beg for a big bag for our bug bog";

let posMatch = /b\[aeiou]g/g;

// console.log(possibilities.match(posMatch)); //\[ 'beg', 'big', 'bag', 'bug', 'bog' ]

Match All Letters in a String

let quoteSample = "The quick brown fox jumps over the lazy dog.";

let alphabetRegex = /\[a-z]/ig;

// console.log(quoteSample.match(alphabetRegex)); // Matches every letter 

Match Characters not Specified

let notSpecified = "3 blind mice.";

let nsRegex = /\[^aeiou0-9]/ig; // Change this line

// console.log(notSpecified.match(nsRegex)); // Change this line

Comments are closed

I invite you to touch base with me on Twitter, Facebook, or LinkedIn to chit-chat about this article.

Moderating comments can be a full time job. Unfortunately, I don't have the time required to keep comments in check. I also do not see the need to sacrifice the speed of this site with some of the freely available commenting systems.