^
and $
anchors to match the start and end of the string, respectively.const regexp = /^abc$/;
// Where 'abc' is the exact string you want to match
^
and $
anchors to match the start and end of the string, respectively.const regexp = /^$/;
\s
meta-sequence to match any whitespace character, including spaces, tabs, newlines, etc.+
quantifier to match one or more occurrences of the previous character.g
) to match all occurrences of the pattern in the string.const regexp = /\s+/g;
\r
character to match carriage returns, the \n
character to match newlines, and the \r\n
sequence to match carriage returns followed by newlines.g
) and multiline (m
) flags to match all occurrences of the pattern in the string.const regexp = /\r|\n|\r\n/gm;
^
) to match any character that is not a word character (\w
) or a whitespace character (\s
).g
) to match all occurrences of the pattern in the string.i
) to match both uppercase and lowercase characters.const regexp = /[^\w\s]/gi;
^
and $
anchors to match the start and end of the string, respectively.a-zA-Z0-9-
pattern to match any alphanumeric character, dashes and hyphens.+
quantifier to match one or more occurrences of the previous character.const regexp = /^[a-zA-Z0-9-_]+$/;
^
and $
anchors to match the start and end of the string, respectively.a-zA-Z\s
pattern to match any letter and whitespace character.+
quantifier to match one or more occurrences of the previous pattern.const regexp = /^[A-Za-z\s]+$/;
^
and $
anchors to match the start and end of the string, respectively.?!
) to match any character that is not followed by the pattern you want to exclude.g
) to match all occurrences of the pattern in the string.|
character to separate them.const regexp = /^((?!(abc|bcd)).)*$/;
// Where 'abc' and 'bcd' are pattern you want to exclude
\(
and \)
characters to match the opening and closing brackets, respectively.+
quantifier to match one or more characters, as needed.g
) to match all occurrences of the pattern in the string.\(
and \)
with \[
and \]
to match square brackets and with \{
and \}
to match curly brackets.const regexp = /\(([^)]+)\)/g;
^
and $
anchors to match the start and end of the string, respectively.const regexp = /^(0?[1-9]|[12][0-9]|3[01])[\/\-](0?[1-9]|1[012])[\/\-]\d{4}$/;
^
and $
anchors to match the start and end of the string, respectively.const regexp = /^(0?[1-9]|[12][0-9]|3[01])[\/\-](0?[1-9]|1[012])[\/\-]\d{4}$/;
.{1,n}
quantifier to match any character between 1
and n
times.g
) to match all occurrences of the pattern in the string.const regexp = /.{1,2}/g;
// Where '2' is the number of characters per chunk
Snippet collection
A collection of cheatsheets to bookmark and come back to whenever you need to look up anything.
JavaScript, String
Escapes a string to use in a regular expression.
JavaScript, String
Regular expressions, while very powerful, are notoriously hard to master. Start using them in your JavaScript code by understanding these 6 features.
JavaScript, String
Escapes a string for use in HTML.