Regular expressions used with JavaScript test for matching patterns. They help you to verify forms are filled out correctly for things like credit card numbers, dates, times, zip codes, email adresses and other things that have a specific format. They can be fairly simple or extremely complex. There are many resources on the internet for finding regular expressions to filter out specific data, so it is not likely that you will need to write your own regular expressions. It is however important that you enderstand what it is they are doing and how to use them.

A regular expression may also referred to as regex or regexp.

 

What JavaScript functions to use with a regular expressions to look for a string

There are two JavaScript functions that are often used with regular expressions.

.search()    this will return the position of the mached string. You would use this to test if there is a match to your regular expression.

var entString = "Canis lupus lycaon";
var testSerStr = entString.search(/lupus/i);
alert(testSerStr); // the alert box will read 6

.replace()    this will search for one string or a match to a regular expression and replace it with the other.

var testRplStr = entString.replace(/lycaon/i, "baileyi");
alert(testRplStr); // the alert box will read Canis lupus baileyi

.match()    this will return the match of a regular expression. You could find the matching string and use it somewhere else in the program.

var testMatStr = entString.match(/lupus/i);
alert(testMatStr); // the alert box will read lupus

.test()       this searches a string for a pattern and returns true if it matches, false if it does not.

var testforstring = /lupus/i;
var testTestStr = testforstring.test(entString);
alert(testTestStr); // the alert box will read true

Modifiers for regular expressions

In addition to the JavaScript commands shown above, you can apply modifiers that are attached to the end of the regular expression after the final forward slash.

i   this will perform case-insensitive matches - upper or lower case

g   this will perform a global match and will find all matches not just the first match

m  this will perform multi-line matches

 

How do you form regular expressions?

A regular expression can find one number or one letter or is can find a complex set of matching letter and number combinations. You might find yourself need to formulate regular expressions for simple tasks, but if you are scratching your head over creating some for form checking, chances are someone else has already come up with the correct combination. Do a little research on the internet before spending too much time on lengthy regular expressions.

Regular expressions are contained inside of forward slashes. For example:

.search(/urban/)  would search for a string that matches this combination of letters
.search(/57/)   would search for a string that contained 57 in that order

Looking for specific patterns, or anything that has any letter or anything that has any number, etc. is covered by by a set of expressions that start with a backward slash. These expresssions are included inside of the set of forward slashes mentioned before.

For example:

var zipcodeTest = /^\d\d\d\d\d$/;  see how the regular expressions are included inside the forward slashes. 

ExpressionSearches for a match to:
. Any one character. Includes: a letter, a number, a space or a symbol
\w Any word character. Includes: a - z, A - Z, 0 - 9 and underscore _
\W Any character that is NOT a word character.
\d Any character that is a digit. Includes: 0 - 9
\D Any character that is NOT a digit. 
\s A space, a carriage return or a new line 
\S Anything that is NOT a space, a carriage return or a new line
\b Looks for word boundaries, so that the string is not contained inside of another word.
^ The beginning of a string. Used so that this string inside another string with characters preceding it. 
$ The end of a string verifying that no other characters come after. Used to find the last characters in a string like .com. 
\b A space, beginning of the string, end of the string, or any other non-letter or non-number character. 
[ ] Any one of the characters contained within the brackets or contained in the range between the brackets.
Example: [abcde], [a-z], [123], [0-9]
[^ ] Any character that is NOT contained between the brackets or NOT contained within the range of character between the brackets.
| Ether one of the characters before OR after the pipe | symbol, but not both of the characters
Example: (m|n) search for m OR n
\ Used to escape any special regex symbol so as not to be interpreted as a regex character
* None or more of the preceding character, class or subpattern
Example: y* will match any string that contains zero or more occurances of the letter y
?

None of one of the preceding character, class or subpattern
Exmple: q? matches any string that contains  zero or more occurances of the letter q

+ One or more of the preceding character, class of subpattern 
Example: c+ will match any string that contains at least one or more of the letter c 
{n} An occurrence of the previous item exactly n times.
{n, } An occurrence of the previous item n times or more
{n,m} An occurrence of the previous item at least n times but not more than m times
i A Modifier - placed after the last forward slash of the regular expression
looks for lower or upper case instances of the regular expression
g A Modifier - placed after the last forward slash of the regular expression
performs a global match - finds all matches - does not stop with the first match
m A Modifier - placed after the last forward slash of the regular expression
performs a multi-line match