DEV Community

Cover image for Regex Cheat Sheet
Emma Bostian ✨
Emma Bostian ✨

Posted on • Updated on

Regex Cheat Sheet

A regular expression, or 'regex', is used to match parts of a string. Below is my cheat sheet for creating regular expressions.


Testing a regex

  • Use the .test() method
let testString = "My test string";
let testRegex = /string/;
testRegex.test(testString);

Testing multiple patterns

  • Use the OR operator (|)
const regex = /yes|no|maybe/;

Ignoring case

  • Use the i flag for case insensitivity
const caseInsensitiveRegex = /ignore case/i;
const testString = 'We use the i flag to iGnOrE CasE';
caseInsensitiveRegex.test(testString); // true

Extracting the first match to a variable

  • Use the .match() function
const match = "Hello World!".match(/hello/i); // "Hello"

Extracting all of the matches in an array

  • Use the g flag
const testString = "Repeat repeat rePeAT";
const regexWithAllMatches = /Repeat/gi;
testString.match(regexWithAllMatches); // ["Repeat", "repeat", "rePeAT"]

Matching any character

  • Use the wildcard character . to be a placeholder for any character
// To match "cat", "BAT", "fAT", "mat"
const regexWithWildcard = /.at/gi;
const testString = "cat BAT cupcake fAT mat dog";
const allMatchingWords = testString.match(regexWithWildcard); // ["cat", "BAT", "fAT", "mat"]

Matching a single character with multiple possibilities

  • Use character classes, which allow you to define a group of characters you wish to match
  • You place them inside square brackets []
// Match "cat" "fat" and "mat" but not "bat"
const regexWithCharClass = /[cfm]at/g;
const testString = "cat fat bat mat";
const allMatchingWords = testString.match(regexWithCharClass); // ["cat", "fat", "mat"]

Match letters of the alphabet

  • Use a range within the character set [a-z]
const regexWithCharRange = /[a-e]at/;
const catString = "cat";
const batString = "bat";
const fatString = "fat";

regexWithCharRange.test(catString); // true
regexWithCharRange.test(batString); // true
regexWithCharRange.test(fatString); // false

Match specific numbers and letters

  • You can also use the hyphen to match numbers
const regexWithLetterAndNumberRange = /[a-z0-9]/ig;
const testString = "Emma19382";
testString.match(regexWithLetterAndNumberRange) // true

Match a single, unknown character

  • To match a set of characters you don't want to have, use the negated character set
  • To negate a character set, use a caret ^
const allCharsNotVowels = /[^aeiou]/gi;
const allCharsNotVowelsOrNumbers = /[^aeiou0-9]/gi;

Match characters that occur one or more times in a row

  • Use the + symbol
const oneOrMoreAsRegex = /a+/gi;
const oneOrMoreSsRegex = /s+/gi;
const cityInFlorida = "Tallahassee";

cityInFlorida.match(oneOrMoreAsRegex); // ['a', 'a', 'a'];
cityInFlorida.match(oneOrMoreSsRegex); // ['ss'];

Matches characters that occur zero or more times in a row

  • Use the asterisk *
const zeroOrMoreOsRegex = /hi*/gi;
const normalHi = "hi";
const happyHi = "hiiiiii";
const twoHis = "hiihii";
const bye = "bye";

normalHi.match(zeroOrMoreOsRegex); // ["hi"]
happyHi.match(zeroOrMoreOsRegex); // ["hiiiiii"]
twoHis.match(zeroOrMoreOsRegex); // ["hii", "hii"]
bye.match(zeroOrMoreOsRegex); // null

Lazy Matching

  • The smallest part of a string that matches the given requirements
  • Regex, by default, are greedy (matches the longest portion of a string meeting the given requirements)
  • Use the ? character to lazy match
const testString = "catastrophe";
const greedyRexex = /c[a-z]*t/gi;
const lazyRegex = /c[a-z]*?t/gi;

testString.match(greedyRexex); // ["catast"]
testString.match(lazyRegex); // ["cat"]

Match starting string patterns

  • To test for a match of characters at the beginning of a string, use the caret ^, but outside of the character set
const emmaAtFrontOfString = "Emma likes cats a lot.";
const emmaNotAtFrontOfString = "The cats Emma likes are fluffy.";
const startingStringRegex = /^Emma/;

startingStringRegex.test(emmaAtFrontOfString); // true
startingStringRegex.test(emmaNotAtFrontOfString); // false

Match ending string patterns

  • Use the dollar sign $ at the end of a regex to check whether a pattern exists at the end of a string
const emmaAtBackOfString = "The cats do not like Emma";
const emmaNotAtBackOfString = "Emma loves the cats";
const startingStringRegex = /Emma$/;

startingStringRegex.test(emmaAtBackOfString); // true
startingStringRegex.test(emmaNotAtBackOfString); // false

Matching all letters and numbers

  • Use the \word shorthand
const longHand = /[A-Za-z0-9_]+/;
const shortHand = /\w+/;
const numbers = "42";
const myFavoriteColor = "magenta";

longHand.test(numbers); // true
shortHand.test(numbers); // true
longHand.test(myFavoriteColor); // true
shortHand.test(myFavoriteColor); // true

Match everything except letters & numbers

  • You can use for the opposite of \w with \W
const noAlphaNumericCharRegex = /\W/gi;
const weirdCharacters = "!_$!!";
const alphaNumericCharacters = "ab283AD";

noAlphaNumericCharRegex.test(weirdCharacters); // true
noAlphaNumericCharRegex.test(alphaNumericCharacters); // false

Match all numbers

  • You can use a character set [0-9], or use the shorthand \d
const digitsRegex = /\d/g;
const stringWithDigits = "My cat eats $20.00 worth of food a week.";

stringWithDigits.match(digitsRegex); // ["2", "0", "0", "0"]

Match all non-numbers

  • You can use the opposite of \d with \D
const nonDigitsRegex = /\D/g;
const stringWithLetters = "101 degrees";

stringWithLetters.match(nonDigitsRegex); // [" ", "d", "e", "g", "r", "e", "e", "s"]

Matching whitespace

  • Use \s to match white space and carriage returns
const sentenceWithWhitespace = "I like cats!"
var spaceRegex = /\s/g;
whiteSpace.match(sentenceWithWhitespace); // [" ", " "]

Matching non-whitespace

  • You can use the opposite of \s with \S
const sentenceWithWhitespace = "C a t"
const nonWhiteSpaceRegex = /\S/g;
sentenceWithWhitespace.match(nonWhiteSpaceRegex); // ["C", "a", "t"]

Matching character counts

  • You can specify a specific number of characters in a row using {lowerBound, upperBound}
const regularHi = "hi";
const mediocreHi = "hiii";
const superExcitedHey = "heeeeyyyyy!!!";
const excitedRegex = /hi{1,4}/;

excitedRegex.test(regularHi); // true
excitedRegex.test(mediocreHi); // true
excitedRegex.test(superExcitedHey); //false

Matching lowest number of character counts

  • You can define only a minimum number of character requirements with {lowerBound,}
  • This is called a quantity specifier
const regularHi = "hi";
const mediocreHi = "hiii";
const superExcitedHey = "heeeeyyyyy!!!";
const excitedRegex = /hi{2,}/;

excitedRegex.test(regularHi); // false
excitedRegex.test(mediocreHi); // true
excitedRegex.test(superExcitedHey); //false

Matching an exact number of character counts

  • You can specify the exact number of character requirements with {requiredCount}
const regularHi = "hi";
const bestHi = "hii";
const mediocreHi = "hiii";
const excitedRegex = /hi{2}/;

excitedRegex.test(regularHi); // false
excitedRegex.test(bestHi); // true
excitedRegex.test(mediocreHi); //false

Matching all or none of a character

  • To check whether a character exists, use the ?
const britishSpelling = "colour";
const americanSpelling = "Color";
const languageRegex = /colou?r/i;

languageRegex.test(britishSpelling); // true
languageRegex.test(americanSpelling); // true

Top comments (53)

Collapse
 
bkichler profile image
Brian Kichler

I go cross-eyed dealing with regex, but this is an excellent reference I'm definitely coming back to. I almost always head to regex101.com whenever pattern-matching starts to devour my brain.

Collapse
 
richardvk profile image
Richard vK

Just shared the link to regex101 with my colleagues today - great tool!

Collapse
 
vasiliib profile image
Burlacu Vasilii

Yep, regex101 helps a lot! It even gives you the code snippet for your programming language (e.g. PHP)

Collapse
 
saul_bt profile image
Saúl Blanco Tejero

RT

Collapse
 
lexlohr profile image
Alex Lohr

Using the | operator is a dangerous habit. You easily forget to guard yourself against matching substrings, for example consider the following line:

const isLocalHost = /127.0.0.1|localhost/.test(location.host);
Enter fullscreen mode Exit fullscreen mode

And now what happens on localhost.mydevious.url? The secure solution is to use ^ and $ to gate the start and end of the tested string.

Also, one trick up JS regex's sleeve is the back reference (\1-\9):

/(["'])(.*?)\1/.test('"I can match Emma\'s full strings"') && RegExp.$2;
/(["'])(.*?)\1/.test("'I can match when Emma say \"things in strings\"'") && RegExp.$2;
Enter fullscreen mode Exit fullscreen mode

This will re-use the (matches) inside the same regex.

Collapse
 
c24w profile image
Chris Watson

The real guard is to write tests to assert the correct behaviour :)

Collapse
 
lexlohr profile image
Alex Lohr

Just don't forget about those edge cases ;-)

Collapse
 
nickytonline profile image
Nick Taylor
Collapse
 
baso53 profile image
Sebastijan Grabar

The funny thing about these Regex cheat cheets is no matter how many of them I put in my bookmarks, I never end up using them.

Collapse
 
colinodell profile image
Colin O'Dell

That's why you need a regex cheat sheet mug!

Regular expression mug

Collapse
 
pflash profile image
Precious adeyinka

sebastian 😂 me too

Collapse
 
mvoloskov profile image
Miloslav 🏳️‍🌈 🦋 Voloskov • Edited

For me, Regex and Perl are write-only languages. I can't debug, edit or even read regex – I just rewrite it from scratch with some tool like RegExr or something. For me, a person who can memorise all that syntax is definitely a superhuman.

Collapse
 
dystroy profile image
Denys Séguret • Edited

Making code readable is half the job of the coder. It's also true for regexes, which means you have to look for ways to separate parts and comment your regexes (or at least name the groups).

Fortunately, you can find solutions in most languages. Here are two examples from some of my OS codes:

In Javascript:

    cmdArgRegex = miaou.lib("rex")`
            ([a-z ]*)                        // options (optional)
            (-?\d*\s*d\s*[\d+-]+)            // left rolldef (mandatory)
            (?:                              // inequation (optional)
                    \s*
                    ([<>=]+)                 // operator
                    \s*
                    ([\w+-]+)                // right rolldef or scalar
            )?
            \s*$
            /i`;

(I think there are JS libs to do that without taking my code, now)

In rust:

        static ref RE: Regex = Regex::new(
            r"(?x)
            ^
            (?P<slash_before>/)?
            (?P<pattern>[^\s/:]+)?
            (?:/(?P<regex_flags>\w*))?
            (?:[\s:]+(?P<verb>\S*))?
            $
            "

(this is the standard way in this language)

Collapse
 
georgewl profile image
George WL

I tend to not bother learning regex, the cases where it's useful, I'll just go to a site like regexr.com and then use their tools to build one.

Collapse
 
jacksonelfers profile image
Jackson Elfers

In think that's the general consensus. I only use it occasionally so it's not worth my time to be 100 percent fluent.

Collapse
 
mtomasek2 profile image
mtomasek2

I'm using regex debuger, regexbuddy.com/, to debug different regex flavors. It saved me a lot of time over the years. It can also run on linux (using Wine).
They also have a tool for building regexps, regexmagic.com/, but I'm not using is so I'm just saying it is out there.

Collapse
 
thomasjunkos profile image
Thomas Junkツ

For the day-to_day development, I recommend using Rubular or Regexly

Collapse
 
phillipdodd profile image
Phillip Dodd

Oh my, I wish that I had encountered this post about two months ago before I began the difficult journey of acquiring what (little) regex knowledge I have now... haha!

I'm absolutely going to be bookmarking this to revisit next time I have the need and will be sharing it with my team. You've laid this out in a very understandable way; thanks for sharing :)

Collapse
 
takumiiweb profile image
Guillaume Duchemin • Edited

Thx a lot for this awesome cheat sheet !
You made a mistake in this case

const regexWithLetterAndNumberRange = /[a-z0-9]/ig;
const testString = "Emma19382";
testString.match(regexWithLetterAndNumberRange) // true

The result of match is // ["E", "m", "m", "a", "1", "9", "3", "8", "2"] ;)

Collapse
 
leo profile image
Leonardo Galante

Great cheat sheet, thank you Emma! Soon I'm gonna make a post about this extension I made for VSCode that could help understanding RegExp: marketplace.visualstudio.com/items...

Collapse
 
mvasigh profile image
Mehdi Vasigh

Great post and doubles as a great practice assessment if you hide all of the code blocks!

Collapse
 
bmaciejewicz profile image
Bartosz Maciejewicz

I <3 it :) thank you very much :)

Collapse
 
plainjavascript profile image
plainJavaScript

Pretty fine.

Collapse
 
girng profile image
I'mAHopelessDev

very neat, thanks!