w3resource logo


JavaScript  Regular Expression

JavaScript : Regular Expression

<<PreviousNext>>

Description

A regular expression (sometimes abbreviated to "regex") is a pattern used to match character combinations in a string.

For example, a regular expression can be used to search for all text lines in a paragraph that contain word "red" and display those lines where the match is found.

You can also substitute the word "red" with "green". Sometimes regular expressions are used to check an email, password, name etc. into a HTML form field to get a valid format. In JavaScript, regular expressions are also objects.

Creating Regular Expression

There are two ways to construct regular expression.

Using an object initializer, as follows :

var colorName = /Green/;

The above code creates a new RegExp object called colorName and assigns the pattern Green. In the above notation the forward slash character (/) is used to designate the starting and end of the pattern.

Using the constructor function, as follows :

re = new RegExp("Green")

JavaScript : Regular Expressions patterns

Sometimes various pattern matching is required instead of direct matching. For example, the pattern /xy*z/ matches any character.

For example, /bo*/ matches 'boo' in "A book" and 'b' in "A beautiful river", but nothing in "A going concern".

The following table provides a complete list and description of the special pattern matching characters that can be used in regular expressions.

Character Meaning
\ Indicates that the next character is special and not to be interpreted literally
For example, /d/ matches the character 'd'. By placing a backslash in front of d, that is by using /\d/, the character becomes special to mean matches any character which is a digit.
-or-
Indicates that the next character is not special and should be interpreted literally.
^ Matches the beginning of the string or line.
For example /^A/ does not match the 'A' in "about Articles" but does match it in "Articles of life"
$ Matches the end of the string or line.
For example, /e$/ does not match the 't' in "exact", but does match it in "w3resource"

* Matches the previous character 0 or more times.
For example, /bo*/ matches 'boo' in "A bootable usb" and 'b' in "A beautiful mind", but nothing in "A going concern".
+ Matches the previous character 1 or more times.
For example, /a+/ matches the 'a' in "Daniel" and all the a's in "Daaam"
? Matches the previous character 0 or 1 time.
For example, /r?eu?/ matches the 're' in "w3resource" and the 'eu' in "europe ."  
. The decimal point matches any single character except a new line.
For example, /.n/ matches 'an' and 'on' in "an orange is on the table".
(x) Matches 'x' and remembers the match character. For example, /(go)/ matches and remembers 'go' in "go there"
x|y Matches either 'x' or 'y'.
For example, /green|red/ matches 'green' in "green color" and blue in "blue color."
{n} Matches exactly n (a positive integer) occurrences of the preceding character.
For example, /a{2}/ doesn't match the 'a' in "dam," but it matches all of the a's in "daam," and the first two a's in "daaam"
{n,} Matches at least n (a positive integer) occurrences of the preceding character.
For example, /a{2,}/ doesn't match the 'a' in "dam," but it matches all of the a's in "daam," and the first two a's in "daaam"  
{n,m} Matches at least n and at maximum m (n and m are positive integer) occurrences of the preceding character.
For example, /a{1,3}/ matches nothing in "dom", the 'a' in "dam," the first two a's in "daam" and the first three a's in "daaaaaam". Notice that when matching "daaaaaam", the match is "aaa" as the maximum value of m is 3 though the original string had more a's in it.
[xyz] Matches any one from the character set, using a hyphen you can specify a range of characters.
For example. [uvwxyz] is the same as [u-z]. The match 'y' in "yellow" and the 'u' in "blue" .
[^xyz] Matches any character that is not enclosed in the brackets, using a hyphen you can specify a range of characters.
For example, [^wxyz] is the same as [^w-z]. They initially match 'b' in "blue" and 's' in "specify".  
[\b] Matches a backspace.
\b Matches a word boundary (position between a word character and a non-word character), such as a space.
For example, /\bn\w/ matches the 'on' in "sooner"
\B Matches the position which is beyond a word character and a non-word character boundary.
For example, /\w\Bn/ matches 'on' in "sooner".
\cX Matches a control character (X) in a string. For example, /\cM/ matches control-M in a string.
\d Matches any character which is a digit. Equivalent to [0-9].
For example, /\d/ or /[0-9]/ matches '2' in "E2 means second example."
\D Matches any character which is a non-digit. Equivalent to [^0-9].
For example, /\D/ or /[^0-9]/ matches 'C' in "E2 means second example."
\f Matches a Form feed
\n Matches a New line
\r Matches a Carriage return.
\s Matches any white space character (including tab, new line, carriage return, form feed, vertical tab). [ \t\n\r\f\v].
For example, /\s\w*/ matches ' apple' in "An apple."
\S Matches any non-white space character. Equivalent to [^ \f\n\r\t\v].
For example, /\S/\w* matches 'An' in "An apple"
\t Matches a tab
\v Matches a vertical tab.
\w Matches any word character (alphanumeric) including the underscore. Equivalent to [A-Za-z0-9_].
For example, /\w/ matches 'g' in "green," '8' in "12.86," and '3' in "3G."
\W Matches any non-word character, equivalent to [^A-Za-z0-9_].
For example, /\W/ or /[^$A-Za-z0-9_]/ matches '$' in "150$"
\n Where n is a positive integer. A back reference to the last sub string matching the n parenthetical in the regular expression
For example, /red(,)\sgreen\1/ matches 'red, green', in "red, green, white, black."
\ooctal
An octal escape value allows to embed ASCII codes into regular expressions.
\xhex An hexadecimal escape value allows to embed ASCII codes into regular expressions.


<<PreviousNext>>