Javascript RegExp objects allow authors to work with regular expressions in Javascript. Regular expressions work similarly in most scripting languages. We supply them with two paramters: (1)the text pattern to match for, and (2)optional flags. Special characters can also be applied to your patterns for enabling a wide range of logic within your regular expressions.
Regular expressions are used with several String methods of Javascript such as match(), replace(), search(), split(). RegExp objects sport built-in properties, methods and special characters that you can explore on this page.
Both approaches above will establish a similar regular expression in Javascript. The literal approach uses forward slashes around the text pattern, while the constructor approach uses quote marks and separates the paramters. If you know the pattern is static and will not change use a literal regular expression to maximize script performance. If you know that the pattern will be dynamic, use the second approach that calls the constructor and explicitly creates a RegExp object.
This next example shows applying flags to your regular expression. We apply a global(g) flag along with an ignore case(i) flag to the regular expression so that all matches of the word "bird" are found and the text casing is not a factor in the pattern match.
You can apply one or more flags to your patterns in order to alter the match logic.
• g - (global) target all matches
• i - ignore the casing of letters
• m - (multiline) performs match over multiple lines("\n" represents a new line)
Consider these placeholder values before you explore the special characters:
abc = primary pattern to match, replace 'abc' with your pattern.
i / j = integers, replace 'i' and 'j' with integers where you see them below.
\ - escapes the normal functionality of special characters to interpret them literally
Syntax: /abc\+/
^ - match pattern where it is the beginning of input (newlines affected using the g and m flags)
Syntax: /^abc/
$ - match pattern where it is the end of input (newlines affected using the g and m flags)
Syntax: /abc$/
* - match pattern 0 or more times
Syntax: /abc*/
+ - match pattern 1 or more times
Syntax: /abc+/
? - match pattern 1 or 0 times
Syntax: /abc?/
. - match everything except newline(\n) characters
Syntax: /./
( ) - match and store into resulting array
Syntax: /(abc)/
(?: ) - match and do not store into resulting array
Syntax: /(?:abc)/
(?= ) - match 'abc' only if it precedes 'def'
Syntax: /abc(?=def)/
(?! )- match 'abc' only if it does not precede 'def'
Syntax: /abc(?!def)/
| - match either 'abc' or 'def'
Syntax: /abc|def/
{i} - match the pattern 'i' times
Syntax: /abc{i}/
{i,} - match the pattern at least but not limited to 'i' times
Syntax: /abc{i,}/
{i,j} - match the pattern at least 'i' times, and at the most 'j' times
Syntax: /abc{i,j}/
[ ] - match the range of characters specified within the brackets
Syntax: /[hijklmnop]/ same as /[h-p]/
[^ ] - match what is not in the character set
Syntax: /[^hijklmnop]/ same as /[^h-p]/
\b - match where 'abc' starts a new word
Syntax: /\babc/
\B - match where a word ends with 'abc'
Syntax: /\Babc/
\c - match a control character ( A - Z )
Syntax: /\cA/
\d - match digit found in a string, same as using [0-9]
Syntax: /\d/
\D - match non-digit found in a string, same as using [^0-9]
Syntax: /\D/
\f - match form feed characters
Syntax: /\f/
\n - match newline characters
Syntax: /\n/
\r - match carriage return characters
Syntax: /\r/
\s - match white space, newline, return and tab, same as using [ \n\r\t]
Syntax: /\s/
\S - match non-white space characters, same as using [^ \n\r\t]
Syntax: /\S/
\t - match tab characters
Syntax: /\t/
\v - match vertical tab
Syntax: /\v/
\w - match any alphanumeric character, same as using [a-zA-Z0-9_]
Syntax: /\w/
\W - match any non-alphanumeric character, same as using [^a-zA-Z0-9_]
Syntax: /\W/
• lastindex - returns the next index position that follows the last match made
• source - returns the text of the pattern without its forward slashes
• exec - returns the matched pattern if it is found, and returns 'null' if not found
• test - returns 'true' if a match is found, and returns 'false' if not found
• toString - returns a string representing the RegExp object