1. Tech

Your suggestion is on its way!

An email with a link to:


was emailed to:

Thanks for sharing About.com with others!

Regular Expressions

1. Pattern Matching

clr gif
Join the Discussion

Questions? Comments?

More of this Feature


Regular expressions are a very powerful tool when it comes to being able to perform field validations, compare values, perform substitutions and numerous other tasks that would otherwize require dozens or hundreds of lines of extra code. Unfortunately for those just starting out in programming the way that regular expressions are constructed and what the various parts of the code that you use with regular expressions actually mean is at least an order of magnitude more difficult than most other parts of the programming language.

In this series of tutorials we are going to look at what the codes used in regular expressions actually mean one piece at a time and clear up that confusion. We'll start by looking at how we can use a regular expression to see if one character string contains one or more occurrences of a shorter string. This use for pattern matching is about as simple as regular expressions get and will introduce you to how you can easily code a regular expression test.

var myString = 'the cat sat on the mat';
var re = /at/;
if (re.test(myString))
return 'one or more occurrences of "at" found';

The first thing to notice is that regular expressions have their value contaied between slash characters rather than placing them in quotes. Javascript knows when we define an object this way that we are creating a regular expression object. In this example we have defined a regular expression object called re that will process using the character string "at".

We can perform a simple pattern match to see if some text contains this character string by using the test method that belongs to the regular expression object. This method will return true if a match is found for the regular expression within the passed text and false if no match is found. In our example there are several occurrences of "at" within the text and so our call returns true.

Our regular expression in this example is a simple text string of two particular characters. In future tutorials we will see how we can substitute special symbolds that can match to particular types of characters that make using regular expressions for pattern matching a very powerful tool.

Related Video
Photoshop Pattern Stamp Tool
Adobe Photoshop Pattern Maker

©2016 About.com. All rights reserved.