1. Technology

Regular Expressions

Regular Expressions are one of the more difficult to understand parts of the Javascript language. This series of tutorials will take you step by step through how regular expressions work and how to code them.
  1. Beyond Javascript

Just what exactly are regular expressions and what are they used for.

Pattern matching
This first tutorial on regular expressions shows you how to define a regular expression and how to use it to test if a text field contains a specified pattern.

Ignore Case and Global
This tutorial on regular expressions shows you how to retrieve all copies of a pattern into an array by using "global" and also how to ignore capitalization when locating the matches to your pattern.

Replacing Matches
Regular Expressions can be used with the replace method associated with string objects to find and replace text.

Spliting Matches
You can use regular expressions with the string object's split method to control how a string is split up into array elements.

Start and End
Having learned the main ways of using regular expressions we now move on to start looking at what can be included in the regular expression itself. We begin by looking at the characters that match the start and end of text.

Meta Characters
The characters that have a special meaning in regular expressions are called "meta characters". Here we look at what all of these characters are and how we can get their normal meanings when we want to include the characters as their normal values.

Character Classes
You can group characters together in a regular expression allowing a match on any one.

Often you want to match a pattern where one or more characters is repeated. Here is how to define the number of repetitions of a character that you want to test for.

Greedy, Reluctant, and Possessive
When you start specifying a variable length pattern to match against then it becomes possible sometimes to match that pattern against shorter or longer sections of the text. Here's how to tell it whether to match the longest or shortest possible section of text.

Predefined Classes
Some character classes are so commonly referenced that they have their own one or two character abbreviation defined.

Special Characters
Some special characters that are not able to be entered directly have their own special escape codes that you can use for them in regular expressions.

ASCII and Unicode
When you want to put any character at all into your regular expression and it is not available directly from the keyboard and doesn't have its own special escape code then you can use the appropriate ASCII or unicode escape code to specify it.

As well as having escape characters allowing you to test for the start and end of the string, regular expressions also have escape characters that allow you to test for word and non-word boundaries.

Regular Expressions are at their most useful when you use groups. The simplest use for groups is to allow you to test for multiple repetitions of a group of characters rather than just an individual character.

Back References
Any groups that you define within your regular expression will save the matching text to the backreference array allowing you to refer to that exact text in your processing.

Non-Capturing Groups
There are a number of different reasons for using groups. Where you need a group that you don't want backreferenced you can define the group as non-capturing.

The Javascript version of Regular Expressions supports the "lookahead" functionality which allows you to test if your pattern is followed or not followed by something. Javascript does not support Lookbehind.

Multiline Mode
Regular Expressions do not normally treat new lines as a special boundary condition. By setting multiline mode you can test for new line boundaries more easily.

The RegExp Object
Regular Expressions are stored in special Javascript objects. You can actually use a normal Javascript object declaration statement to define a regular expression if you prefer.

properties of RegExp
There are quite a few properties belonging to the RegExp object. Some we have met before.

More Properties of RegExp
There are also a number of properties that RegExp has available to us that we haven't looked at before.

RegExp Methods
All of the methods that you can use with regular expression objects.

String Methods
A list of all of the methods you can use with string objects that use regular expressions.

Pattern Reference
A summary of all of the different regular expression patterns and what they mean complete with links back to the tutorials that provide additional information about the specific patterns.

©2014 About.com. All rights reserved.