Skip to content

Instantly share code, notes, and snippets.

@heranYang93
Last active April 12, 2024 16:44
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save heranYang93/cd6de552271c8d6a019eee00fa2ac529 to your computer and use it in GitHub Desktop.
Save heranYang93/cd6de552271c8d6a019eee00fa2ac529 to your computer and use it in GitHub Desktop.
Regex

REGEX Cheatsheet

This is a Regex cheatsheet

Summary

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.

Table of Contents

Regex

Examples

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+/>)$/

Character Classes

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*/

Assertions

Anchors mark the position

char function example Regex
^ beginning Javascript /^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 jacket /\Bck/gi

Grouping Constructs

Group the characters

char function example Regex
x|y or red apple /green|red/
[xyz][a-c] any enclosed Javascript /[abcd]/
[^xyz][^a-c] NONE enclosed Javascript /[^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>/

Quantifiers

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 Expressions

Bracket function example Regex
[] a set of character Elephant /[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]+)/

The OR Operator

Bracket function example Regex
(|) or One Two three four /(one|two)/

Flags

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

Character Escapes

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

Author

hy - https://github.com/heranYang93

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment