1. Pattern Matching
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/;
return 'one or more occurrences of "at" found';
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.