category: JavaScript on May, 16 2019
Regex Cheat Sheet
A regular expression, or just regex
, is used to match patterns within the given string. Bellow is the cheat sheet of string matching I find useful. Enjoy!
Testing Methods
- Method
test
- Executes a search for a match within a given string. Returns true or false
/string/.test("My string"); // outputs true
- Method
match
- Searches a given string for a match, and returns the matches in form of an array
"My string".match(/string/); // outputs "string" as the first match
// ["string", index: 3, input: "My string", groups: undefined]
Globals
Ignoring Case
- The
i
flag - Ignores case within the string, makes the regex case insensitive
const str = "Find MY STriNg";
const regex = /my string/i;
str.match(regex); // "MY STriNg"
Global Lookup
- The
g
flag - Searches for a pattern throughout the string and returns an array of found items
const str = "This this THis THIS";
const regex = /this/gi;
str.match(regex); // ["This", "this", "THis", "THIS"]
Pattern Matching
- Starts with operator
^
- Matches the characters at the beginning of a string
const str = "My String";
const regex = /^My/;
str.match(regex); // "My"
- Ends with operator
$
- Matches the characters at the end of a string
const str = "String My";
const regex = /My$/;
str.match(regex); // "My"
- The dot (wildcard) operator
.
- Matches any character, except newline
const str = "Wildcard find yard, card";
const regex = /.ard/gi;
str.match(regex); // ["card", "yard", "card"]
- The or operator
|
- Matches multiple patterns within a given string
const str = "My string to test, find this and that";
const regex = /this|that/gi;
str.match(regex); // ["this", "that"]
- The word operator
\w
- Matches all letters
const str = "My String12 2String";
const regex = /\w/gi;
str.match(regex); // ["M", "y", "S", "t", "r", "i", "n", "g", "1", "2", "2", "S", "t", "r", "i", "n", "g"]
- The not word operator
\W
- Matches all non-letter characters including whitespace
const str = "My String _$*! 2String";
const regex = /\W/gi;
str.match(regex); // [" ", " ", "$", "*", "!", " "]
- The digit operator
\d
- Matches all digits
const str = "My String12 2String";
const regex = /\d/gi;
str.match(regex); // ["1", "2", "2"]
- The not digit operator
\D
- Matches all non-digit characters including whitespace
const str = "My String12 2String";
const regex = /\D/gi;
str.match(regex); // ["M", "y", " ", "S", "t", "r", "i", "n", "g", " ", "S", "t", "r", "i", "n", "g"]
- The whitespace operator
\s
- Matches all whitespaces
const str = "My String12 2String";
const regex = /\s/g;
str.match(regex); // [" ", " "]
- The not whitespace operator
\S
- Matches all except whitespaces
const str = "My String12 2String";
const regex = /\S/g;
str.match(regex); // ["M", "y", "S", "t", "r", "i", "n", "g", "1", "2", "2", "S", "t", "r", "i", "n", "g"]
The list operator []
The output depends on what you put in between the brackets.
- Match any of
const str = "lit fit git";
const regex = /[lfg]it/g;
str.match(regex); // ["lit", "fit", "git"]
- Match between characters
const str = "lit fit git";
const regex = /[g-l]it/g;
str.match(regex); // ["lit", "git"]
- Match numbers and letters
const str = "My String 123 % test *!";
const regex = /[a-z0-9]/gi;
str.match(regex); // ["M", "y", "S", "t", "r", "i", "n", "g", "1", "2", "3", "t", "e", "s", "t"]
Quantifiers
Quantifiers can be greedy or lazy, depending on the regex definition. A greedy quantifier causes the regex engine to match as many occurrences of the given patterns as possible and vice versa, a lazy quantifier causes the engine to match as few occurrences as possible. Some of the greedy quantifiers are ?
, \?
, *
, +
and some of the lazy ones are ??
, *?
, +?
.
- Match zero or more
*
const str = "code";
const str2 = "codeeeee";
const str3 = "codeecode";
const str4 = "nomore";
const regex = /code*/gi;
str.match(regex); // ["code"]
str2.match(regex); // ["codeeeee"]
str3.match(regex); // ["codee", "code"]
str4.match(regex); // null
- Match one or more
+
const str = "codeeeee";
const str2 = "codede";
const regex = /e+/gi;
const regex2 = /d+/gi;
str.match(regex); // ["eeeee"]
str2.match(regex2); // ["d", "d"]
- Match zero or one
?
const str = "codeeeee";
const regex = /cod[a-z]?/gi;
str.match(regex); // ["code"]
- Match n times
{n}
const str = "codeeeee";
const regex = /cod[a-z]{3}/gi;
str.match(regex); // ["codeee"]
- Match atleast n times
{n,}
const str = "codeeeee";
const regex = /cod[a-z]{2,}/gi;
str.match(regex); // ["codeeeee"]
- Match between n and m times
{n,m}
const str = "codeeeeee";
const regex = /cod[a-z]{1,3}/gi;
str.match(regex); // ["codeee"]
Hostek offers high-quality service and extremely reliable server management, no matter your business size or desired platform. It has been my #1 pick for years 99.999% Uptime & Money Back Guarantee
Teachable is the platform helping entrepreneurs to build and sell online courses. Set up video, audio, and text; collect payments; and communicate with students with ease
Subscribe to get the latest posts delivered right to your inbox