Javascript Regular Expression Syntax
Regular expressions are a type of value that can match with strings.
The examples below use the match method. Visit methods.html to see other methods which can utilize regular expressions.
A regular expression or regex is bracketed by backslashes.
The regex below can match with strings containing "abc".
You can also create a regex with the RegExp constructor.
The RegExp constructor lets you make regexes out of variables.
Special characters will still be interpreted as special characters.
Escape the special characters.
Certains characters, called tokens, have special meaning in regexes. For example, a period will match any single character.
\d matches digits.
\D matches nondigits.
\w will match digits, alphabetic characters, and underscores.
\W matches all other characters.
\s matches whitespace characters such as spaces, tabs, and line feeds.
\S matches nonwhitespace characters.
\S matches nonwhitespace characters.
A vertical bar ("|") allows you to match on multiple patterns.
A "+" will match one or more of the preceding character.
A "?" modifier will match zero or one of the preceding character.
And a wildcard ("*") will match zero or more of the preceding character.
Wildcards are greedy by default. They will match as many characters as possible. Use a '?' modifier with '*' to make a nongreedy match.
Use curly braces to match a character only 'n' times.
Use curly braces to match a character at least 'n' times.
Use curly braces to match a character 'n' times, but not more than 'm' times.
"^" matches the beginning of the string.
"$" matches the end of the string.
The lookahead modifier will check if another pattern follows the pattern you're looking for, but won't include the second pattern.
A negative lookahead checks that the second pattern doesn't follow.
Square brackets let you define your own group of characters.
A "^" at the start of the range will negate that class.
Backreferences allow you to utilize matches.
Please visit methods.html to see methods which can utilize regular expressions.