Javascript Reference Guide

 
 
This guide contains useful Javascript tips.

rss feed






Regular expression.



Information
Regular expressions are patterns which are used to math portions of a string. A regular expression is written in the form of /pattern/modifiers where "/pattern/" is the regular expression itself, and "modifiers" are a series of characters indicating various options. The regular expression must always be enclosed between / and /. The "modifiers" part is optional. To create the regular expression special characters are used.

Here below you will find three tables. The first table contains the special characters, the second the modifiers and the last table contains the methods you can use.


Special Characters Description
\ Toggles or escapes between literal and special characters.
^ Beginning of string.
$ End of string.
* Zero or more times.
+ One or more times.
? Zero or one time.
. Any character except newline.
\b \b represents a word boundary. A word boudary is any character which is not a letter, number or an underscore.

For example: ! @#\$%^&*()-+=~:;<>,.|{}[]/

For example, "fred has a red car.", the regular expression /\bre/ would match the 're' in 'red', not the one in 'Fred'.
\B \B represents a non-word boundary. A non-word boundary is any character which is a letter, number or an underscore.

For example, "fred has a red car.", the regular expression /\Bre/ would match the 're' in 'fred', not the one in 'red'.
\d Any decimal digit 0-9 (same as [0-9]).
\D Any non-digit (same as [^0-9]).
\f Formfeed.
\n Newline.
\r Carriage return.
\s Any space character (same as [ \t\v\n\r\f]).
\S Any non-space character (same as [^ \t\v\n\r\f]).
\t Tab.
\v Vertical Tab.
\w \w represents a word. A word consist of one or more characters which is a letter, number or an underscore (same as [a-zA-Z_0-9]).
\W \W represents a non-word. A non-word consist of one or more characters which is not a letter, number or an underscore (same as [^a-zA-Z_0-9]).
\xnn ASCII character defined by hex code nn.
\nnn ASCII character defined by octal code nnn.
\cX Matches a Ctrl-X character. E.g: \cA matches control-A.
[abc] Any character in the enclosed set.
[^abc] Any character not in the enclosed set.
[a-e] Any character in the enclosed range.
[\b] Matches backspace character.
{n} Exactly n occurrences of the previous character.
{n,} At least n occurrences of the previous character.
{n,m} Range between n and m occurrences of the previous character.
() Grouping that is stored for later use.
a|b Either a or b.
\0 NULL character.




Modifiers Description
/g Do global pattern matching.
/i Do case-insensitive pattern matching.


Methods Description
exec()

regex.exec(string)
If the given string matches the regex, a result array is returned else null is returned.

If at least one match is found, the method returns an array with the following properties:

index (the zero-based index of the start of the match)
input (the original string, str)
[0] (the portion of the string that was matched last)
[1], [2], ..., [n] (the parenthesized substring matches, if such exist)
match()

string.match(regex)
If the given string matches the regex, a result array is returned else null is returned.

If at least one match is found, the method returns an array with the following properties:

index (the zero-based index of the start of the match)
input (the original string, str)
[0] (the portion of the string that was matched last)
[1], [2], ..., [n] (the parenthesized substring matches, if such exist)

Note: This method is the same as exec(), but its object is a string, and its argument is a regular expression.
replace()

string.replace(regex,string)
If the given string matches the regex, it replaces the matches with the given string, and returns the edited string.
search()

string.search(regex)
If the given string matches the regex, it returns the index of the beginning of the match if found, -1 if not.
split()

string.split(regex)
To split the string into an array of strings using a regular expression to determine the positions at which the string is splitted.
test()

regex.test(string)
If the given string matches the regex, it returns true, false if not.