Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save isogram/8ceeda931baf2fc4da1c to your computer and use it in GitHub Desktop.
Save isogram/8ceeda931baf2fc4da1c to your computer and use it in GitHub Desktop.
# SYNTAX:
var pattern = new RegExp(pattern, attributes); # attributes: g (global); i (case-sensitive); m (multiline matches)
var pattern = /pattern/attributes; # same as above
# BRACKETS:
[...]: Any one character between the brackets.
[^...]: Any one character not between the brackets.
[0-9]: It matches any decimal digit from 0 through 9.
[a-z]: It matches any character from lowercase a through lowercase z.
[A-Z]: It matches any character from uppercase A through uppercase Z.
[a-Z]: It matches any character from lowercase a through uppercase Z.
# QUANTIFIERS:
p+: It matches any string containing at least one p.
p*: It matches any string containing zero or more ps.
p?: It matches any string containing one or more ps.
p{N}: It matches any string containing a sequence of N ps.
p{2,3}: It matches any string containing a sequence of two or three ps.
p{2, }: It matches any string containing a sequence of at least two ps.
p$: It matches any string with p at the end of it.
^p: It matches any string with p at the beginning of it.
# LITERAL CHARACTERS:
Alphanumeric: Itself.
\0: The NUL character (\u0000).
\t: Tab (\u0009).
\n: Newline (\u000A).
\v: Vertical tab (\u000B).
\f: Form feed (\u000C).
\r: Carriage return (\u000D).
# METACHARACTERS:
.: a single character.
\s: a whitespace character (space, tab, newline).
\S: non-whitespace character.
\d: a digit (0-9).
\D: a non-digit.
\w: a word character (a-z, A-Z, 0-9, _).
\W: a non-word character.
[\b]: a literal backspace (special case).
[aeiou]: matches a single character in the given set.
[^aeiou]: matches a single character outside the given set.
(foo|bar|baz): matches any of the alternatives specified.
# REGULAR EXPRESSIONS PROPERTIES:
constructor: Specifies the function that creates an object prototype.
global: Specifies if the "g" modifier is set.
ignoreCase: Specifies if the "i" modifier is set.
lastIndex: The index at which to start the next match.
multiline: Specifies if the "m" modifier is set.
source: The text of the pattern.
# REGULAR EXPRESSIONS METHODS:
exec(): Executes a search for a match in its string parameter.
test(): Tests for a match in its string parameter.
toSource(): Returns an object literal representing the specified object; you can use this value to create a new object.
toString(): Returns a string representing the specified object.
# STRING METHODS:
String.match(pattern): Matches given string with the RegExp. Returns an array containing the matches, a string or null.
String.search(pattern): Matches RegExp with string and returns the index of the beginning of the match if found, -1 if not.
String.replace(pattern,string): Replaces matches with the given string, and returns the edited string.
String.split(pattern): Cuts a string into an array, making cuts at matches.
# 8 REGULAR EXPRESSIONS YOU SHOULD KNOW:
Matching a Username: /^[a-z0-9_-]{3,16}$/
Matching a Password: /^[a-z0-9_-]{6,18}$/
Matching a Hex Value: /^#?([a-f0-9]{6}|[a-f0-9]{3})$/
Matching a Slug: /^[a-z0-9-]+$/
Matching an Email: /^([a-z0-9_\.-]+)@([\da-z\.-]+)\.([a-z\.]{2,6})$/
Matching a URL: /^(https?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?$/
Matching an IP Address: /^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/
Matching an HTML Tag: /^<([a-z]+)([^<]+)*(?:>(.*)<\/\1>|\s+\/>)$/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment