This is a Regex cheatsheet
A regular expression is a sequence of characters that defines a search pattern for a character combination. Such sequence is composed by preset notations (meta and literal characters) that form conditions to narrow a larger text down to the text of search.
This cheatsheet provides an overview of regex meta characters in the context of Javascript.
- Examples
- Character Classes
- Assertions
- Grouping Constructs
- Quantifiers
- Bracket Expressions
- The OR Operator
- Flags
- Character Escapes j
function | example | Regex |
---|---|---|
Matching a Hex Value | #FF5733 | /^#?([a-f0-9]{6}|[a-f0-9]{3})$/gi |
Matching an Email | test.1111@gmail.com | /^([a-z0-9_.-]+)@([\da-z.-]+).([a-z.]{2,6})$/ |
Matching a URL | https://regex101.com/ | /^(https?://)?([\da-z.-]+).([a-z.]{2,6})([/\w .-])/?/ |
Matching an HTML Tag | <div>this is a div</div> |
/^<([a-z]+)([^<]+)(?:>(.)</\1>|\s+/>)$/ |
-
Break down (URL example: https://github.com/heranYang93/REGEX-cheatsheet)
1° 2° function function ^ assertion: asserts position at start of the string https://
github.com/heranYang93/REGEX-cheatsheet()? 1st capturing group; the presence of this first group is NOT mandatory https://
github.com/heranYang93/REGEX-cheatsheet^() Must start with this group of characters https://
github.com/heranYang93/REGEX-cheatsheethttp must contain characters-http http
s://github.com/heranYang93/REGEX-cheatsheets? there might be an 's' http s
://github.com/heranYang93/REGEX-cheatsheet:// ':'+'/'+'/' https ://
github.com/heranYang93/REGEX-cheatsheet() 2nd capturing group; the presence of this first group is mandatory https:// github
.com/heranYang93/REGEX-cheatsheet[]+ search for more than 1 letters that belong to the characters in [] https:// github
.com/heranYang93/REGEX-cheatsheet\d numbers https://github.com/heranYang93/REGEX-cheatsheeta-z alphabetic https:// github
.com/heranYang93/REGEX-cheatsheet\. .(dot) https://github.com/heranYang93/REGEX-cheatsheet- -(dash) https://github.com/heranYang93/REGEX-cheatsheet\. .(dot) https://github`.`com/heranYang93/REGEX-cheatsheet () 3rd capturing group; the presence of this first group is mandatory https://github.`com`/heranYang93/REGEX-cheatsheet [a-z.]{2,6} search for 2-6 letters that belong to [] https://github.`com`/heranYang93/REGEX-cheatsheet ()* 4th. 5th, 6th ... capturing group https://github.com`/heranYang93`/REGEX-cheatsheet ()* 4th. 5th, 6th ... capturing group https://github.com/heranYang93`/REGEX-cheatsheet` [\/\w /.-]* 0 to many letters that belong to [] https://github.com`/heranYang93/`REGEX-cheatsheet \/ '/' https://github.com`/`heranYang93/REGEX-cheatsheet /w alphabetic https://github.com/`heranYang93`/REGEX-cheatsheet https://github.com/heranYang93/REGEX-cheatsheet \. .(dot) https://github.com/heranYang93/REGEX-cheatsheet`` - -(dash) https://github.com/heranYang93/REGEX`-`cheatsheet \/? '/', presence NOT mandatory https://github.com/heranYang93/REGEX-cheatsheet $ assertion: asserts position at the end of the string https://github.com/heranYang93/REGEX-cheatsheet -- ^ assertion: asserts position at start of the string -- (https?://) --- ()
Characters, special characters and signs position
topic | function | example | Regex | Alternative |
---|---|---|---|---|
. | any single character | yes make my day |
/.y/ | |
\d | any digit | R2 -D2 |
/.\d-.\d/ | /[0-9]/ |
\D | NOT digit |
Javascript |
/\D/ | /[^0-9]/ |
\w | alphanumeric + _ | Javascadfadsf123123ript |
/\w/ | /[A-Za-z0-9_]/ |
\W | NOT alphanumeric + _ |
Javascadfads'" f123123ript |
/\W/ | /[^a-za-z0-9_]/ |
\s | white space | foo bar |
/\s \w*/ |
|
\S | OTHER THAN white space |
foo bar |
/\S \w*/ |
Anchors mark the position
char | function | example | Regex |
---|---|---|---|
^ | beginning | J avascript |
/^J/ |
$ | end | Javascript |
/t$/ |
x(?=y) | look ahead | Jack Sprat |
/Jack(?=\sSprat|\sFrost)/ |
x(?!y) | NEGATIVE look ahead | Jack Frost | /Jack(?!\sSprat|\sFrost)/ |
(?<=y)x | look behind | Jack Sprat |
(?<=Jack\s|Tom\s)Sprat |
(?<!y)x | NEGATIVE look behind | Jack Sprat | (?<!Jack\s|Tom\s)Sprat |
\b | word boundary | Bojack's jacket |
\bjack |
\B | NONE word boundary | Bojack 's CK jack et |
/\Bck/gi |
Group the characters
char | function | example | Regex |
---|---|---|---|
x|y | or | red apple |
/green|red/ |
[xyz][a-c] | any enclosed | Ja vascript |
/[abcd]/ |
[^xyz][^a-c] | NONE enclosed | J avascript |
/[^abcd]/ |
(x) | match groups (matchAll) | foo bar bar bar |
/(bar)/ |
\k | matching the Named capture group | Do you copy? Sir, yes Sir |
/(?<title>\w+), yes \k<title>/ |
char | function | example | Regex |
---|---|---|---|
x* | 0 or more repetition | boooooed or bird |
/bo*/ |
x+ | 1 or more repetition | boooooed but NOT bird |
/bo+/ |
x? | 0 or 1 | no one cares if is here |
/no one cares if( Ben)? is here/ |
x{n,m} | repeat times | See bracket expressions | - |
Bracket | function | example | Regex |
---|---|---|---|
[] | a set of character | Elepha nt |
/[a-d]/ |
{} | match times (2 to 4 times) | Ba-ba-ba-``ba-``ba- nana |
/(ba\-){1,3}/ |
() | remembered matches | 'Firsty McLastname''Firsty McLastname'.match(/([A-Za-z]+)\s([A-Za-z]+)/) // -> matches 'First McLastname' with 'Firsty' remembered as $1 and 'McLastname' as $2 | /([A-Za-z]+)\s([A-Za-z]+)/ |
Bracket | function | example | Regex |
---|---|---|---|
(|) | or | One Two three four |
/(one|two)/ |
Flags are declared after / to allow for special functionalities Common flags are as follow ...
function | Flag |
---|---|
multiline | m |
global | g |
case-insensitive | i |
Generate indices for substring matches | d |
The backslash in a regular expression precedes a literal character. Certain letters that represent common character classes can be escaped, such as \w for a word character or \s for a space.
escaped characters | Flag |
---|---|
\ | a single backslash \ |
/ | a single forwardslash \ |
\A | start of a string |
\b | word boundary |
\B | not word boundary |
\d | [0-9] |
\D | [^0-9] |
\l | [a-z] |
\L | [^a-z] |
\u | [^A-Z] |
\U | [^A-Z] |
\w | [a-Z0-9_] |
\W | [^a-Z0-9_] |
\r | return |
\s | space |
\S | NOT space |
\S | NOT space |
\Q | ignore escaped characters until \E is found |
\E | stop processing escaped characters |