Skip to content

Instantly share code, notes, and snippets.

@colormono
Last active April 8, 2019 14:24
Show Gist options
  • Save colormono/824f9179d3f43d66f78c78d30fca7a3f to your computer and use it in GitHub Desktop.
Save colormono/824f9179d3f43d66f78c78d30fca7a3f to your computer and use it in GitHub Desktop.
[Regex] #JS
// https://medium.com/factory-mind/regex-tutorial-a-simple-cheatsheet-by-examples-649dc1c3f285
// Trim spaces
^[\s]*(.*?)[\s]*$
// HTML Tag
<([a-z]+)([^<]+)*(?:>(.*)<\/\1>|\s+\/>)
// Anchors — ^ and $
^The matches any string that starts with The -> Try it!
end$ matches a string that ends with end
^The end$ exact string match (starts and ends with The end)
roar matches any string that has the text roar in it
// Quantifiers — * + ? and {}
abc* matches a string that has ab followed by zero or more c -> Try it!
abc+ matches a string that has ab followed by one or more c
abc? matches a string that has ab followed by zero or one c
abc{2} matches a string that has ab followed by 2 c
abc{2,} matches a string that has ab followed by 2 or more c
abc{2,5} matches a string that has ab followed by 2 up to 5 c
a(bc)* matches a string that has a followed by zero or more copies of the sequence bc
a(bc){2,5} matches a string that has a followed by 2 up to 5 copies of the sequence bc
// OR operator — | or []
a(b|c) matches a string that has a followed by b or c -> Try it!
a[bc] same as previous
// Character classes — \d \w \s and .
\d matches a single character that is a digit -> Try it!
\w matches a word character (alphanumeric character plus underscore) -> Try it!
\s matches a whitespace character (includes tabs and line breaks)
. matches any character -> Try it!
\D matches a single non-digit character -> Try it!
\$\d matches a string that has a $ before one digit -> Try it!
// Grouping and capturing — ()
a(bc) parentheses create a capturing group with value bc -> Try it!
a(?:bc)* using ?: we disable the capturing group -> Try it!
a(?<foo>bc) using ?<foo> we put a name to the group -> Try it!
// Bracket expressions — []
[abc] matches a string that has either an a or a b or a c -> is the same as a|b|c -> Try it!
[a-c] same as previous
[a-fA-F0-9] a string that represents a single hexadecimal digit, case insensitively -> Try it!
[0-9]% a string that has a character from 0 to 9 before a % sign
[^a-zA-Z] a string that has not a letter from a to z or from A to Z. In this case the ^ is used as negation of the expression -> Try it!
// Greedy and Lazy match
<.+?> matches any character one or more times included inside < and >, expanding as needed -> Try it!
// Boundaries — \b and \B
\babc\b performs a "whole words only" search -> Try it!
\b represents an anchor like caret (it is similar to $ and ^) matching positions where one side is a word character (like \w) and the other side is not a word
\Babc\B matches only if the pattern is fully surrounded by word characters -> Try it!
// Back-references — \1
([abc])\1 using \1 it matches the same text that was matched by the first capturing group -> Try it!
([abc])([de])\2\1 we can use \2 (\3, \4, etc.) to identify the same text that was matched by the second (third, fourth, etc.) capturing group -> Try it!
(?<foo>[abc])\k<foo> we put the name foo to the group and we reference it later (\k<foo>). The result is the same of the first regex -> Try it!
// Look-ahead and Look-behind — (?=) and (?<=)
d(?=r) matches a d only if is followed by r, but r will not be part of the overall regex match -> Try it!
(?<=r)d matches a d only if is preceded by an r, but r will not be part of the overall regex match -> Try it!
// Hexadecimal value — try it!
// Matches any valid hex color inside text
\B#(?:[a-fA-F0–9]{6}|[a-fA-F0–9]{3})\b
// Valid email (RFC5322) 
\b[\w.!#$%&’*+\/=?^`{|}~-]+@[\w-]+(?:\.[\w-]+)*\b
// Username (simple)
// Minimum length of 3, maximum length of 16, composed by letters, numbers or dashes.
/^[a-z0-9_-]{3,16}$/
// Strong password 
(?=^.{6,}$)((?=.*\w)(?=.*[A-Z])(?=.*[a-z])(?=.*[0-9])(?=.*[|!"$%&\/\(\)\?\^\'\\\+\-\*]))^.*
// URL (http, https or ftp) 
^(((https?|ftp):\/\/)?([\w\-\.])+(\.)([\w]){2,4}([\w\/+=%&_\.~?\-]*))*$
// IPv4 address
\b(?:(?:25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(?:25[0-5]|2[0-4]\d|[01]?\d\d?)\b
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment