In short, regular expressions in programming languages are used for searching or extracting wanted character(s) in a pattern and from a given expression. These procedures are very handy for search forms, forms evaluations and similar projects.

In JavaScript regular expressions are supported via a RegExp reference type. The syntax is similar to other languages that use expressions, such as PHP or Perl and it goes like this:

var expression = /pattern/modifiers;

In the above example, the pattern may be made of any simple or complicated regular expressions, such as characters, classes, groupings, quantifiers, etc... 

Modifiers

Modifiers may be added to the expression as well. If they do exist, then they indicate how the expression should behave. Following modifiers are valid:

  • g modifier indicates that the pattern will be applied to all of the examined string instead of just returning the result after the first match was found;
  • i modifier indicates that the case-insensitive search mode should be applied;
  • m modifier indicates that the multiline search mode will used and the pattern will continue looking after the end of line is reached (line of text).

Here are some examples of simple patterns that might be used:

// Match all instances of "ig" in a string

var pattern1 = /ig/g;

// Match the first instance of "big" or "dig" regardless the case:

var pattern2 = /[bd]ig/i;

// Match all instances that end up with "ig":

var pattern3 = /(ig)$/g;

Meta characters, brackets and quantifiers

As seen above, some characters ("$") may be used to enhance and facilitate the pattern. These characters are called meta characters. Meta characters, in combination with brackets (groupings) and / or quantifiers may be very powerful tool in regular expressions.

Following table shows possible bracket combinations, quantifiers and meta characters:

Brackets are used to find range of characters

Expression Description
[abc] Find any character between the brackets.
[^abc] Find any character not between the brackets.
[0-9] Find any digit from 0 to 9.
[A-Z] Find any character from uppercase A to uppercase Z.
[a-z] Find any character from lowercase a to lowercase z.
[A-z] Find any character from uppercase A to lowercase z.
(abcxyz) Find any character in the given set.
(^abcxyz) Find any character outside the given set.
(x|y|z) Find any of the alternatives specified but not all.

Quantifiers are used to find characters based on the numerical range given within the pattern

Quantifier Description
n+ Matches any string that contains at least one n.
n* Matches any string that contains zero or more occurrences of n.
.n? Matches any string that contains zero or one occurrences of n.
n{X} Matches any string that contains a sequence of X n's.
n{X,Y} Matches any string that contains a sequence of X to Y n's.
n{X,} Matches any string that contains a sequence of at least X n's.
n$ Matches any string with n at the end of it.
^n Matches any string with n at the beginning of it.
?=n Matches any string that is followed by a specific string n.
?!n Matches any string that is not followed by a specific string n.

Metacharacters are characters with a special meaning

Metacharacter Description
. Find a single character, except newline or line terminator.
\w Find a word character (including digits and underscore).
\W Find a non-word character.
\d Find a digit.
\D Find a non-digit character.
\s Find a whitespace character.
\S Find a non-whitespace character.
\b Find a match at the beginning/end of a word.
\B Find a match not at the beginning/end of a word.
\0 Find a NULL character.
\n Find a new line character.
\f Find a form feed character.
\r Find a carriage return character.
\t Find a tab character.
\v Find a vertical tab character.
\xxx Find the character specified by an octal number xxx.
\xdd Find the character specified by a hexadecimal number dd.
\uxxxx Find the Unicode character specified by a hexadecimal number xxxx.

If a meta character will be used as part of a pattern it has to be escaped by a backlash (\); for instance:

// Match all instances that end up with "ig":

var pattern3a = /(ig)$/g;

// Match all instances of "(ig)$":

var pattern3b = /\(ig\)\$/g;

RegExp constructor

Besides defining regular expressions in a literal way (as above), they may be defined as a constructor as well.

In that case the syntax must be written as follows:

var expression = new RegExp("pattern", "modifiers");

Or in a real example:

// Match all instances of "ig" in a string:

var pattern1a = /ig/g;    //literal

var pattern1b = new RegExp("ig", "g"); //constructor

The RegExp type replaces the literal (/      /) with the string ("     "). Because it uses a string, all meta characters need to be double-escaped. For instance:

var pattern1a = /\(ig\)\$/g;   //literal

var pattern1b = new RegExp("\\(ig\\)\\$", "g"); //constructor

To learn how to apply regular expressions by using RegExp integrated properties and methods visit following chapters.

 

›› go to examples ››