Send Close Add comments: (status displays here)
Got it!  This site "robinsnyder.com" uses cookies. You consent to this by clicking on "Got it!" or by continuing to use this website.  Note: This appears on each machine/browser from which this site is accessed.
Regular expressions
by RS  admin@robinsnyder.com : 1024 x 640


1. Regular expressions: Introduction
A regular expression is an expression that can be used to recognized whether a string of characters matches a pattern specified by the string.

Note: The theory goes much deeper than this, but this is all we need to know for the present purposes.

We will now cover some applications, which are much simpler than the theory.

Many programming languages, including JavaScript, VBScript, PERL, etc., support regular expressions.

One use of regular expressions is to validate data entered by users into forms.

2. Common uses
Here are some common uses of regular expressions.

3. Literals
Regular expression string literals in JavaScript are delimited by the forward slash character. The standard backslash characters in string literals apply (e.g., \n for newline).

Some examples are now used. For simplicity, we only cover a subset of the available patterns here.

4. Special characters
Here are some special characters. To get a character within a literal, use the backslash character "\" before that character.


5. Repeating characters


The pattern {n}, where n is a positive integer, matches exactly n occurrences of the preceding character.

The vertical bar "|" is used for disjunction (i.e., the logical "or").

6. Character ranges
Square brackets delimit character ranges for matches.

The pattern [xyz] matches any of the enclosed characters.

The pattern [^xyz] matches any characters not enclosed in brackets.

The pattern [0-9] matches any digit character from 0 to 9.

The pattern [0-9] is the same as 0|1|2|3|4|5|6|7|8|9.

The pattern [A-Z] matches any character from A to Z.

The pattern [0-9,A-Z,a-z] matches any digit or uppercase or lowercase alphabetic character.

7. Grouped patterns
Any regular expression enclosed in parentheses is remembered and can be accessed via the array elements starting at 1 or via the predefined names $1 to $9.


8. Example pattern
What does the following pattern recognize?
[0-9]{5}(\-[0-9]{4})?


9. Getting started
One way to start is to create some patterns that are matched by the regular expression. For optional parts (zero or one) create an instance for each optional part.
12345 12345-6789

Note: One can work the other way to get a regular expression from patterns.

10. Example pattern
What does the following pattern recognize?
[A-Z,a-z][A-Z,a-z,0-9]*


11. Example pattern
What does the following pattern recognize?
bc(.)*xyz$

Try creating regular expressions for the following patterns.

12. Credit card numbers

13. Expiration year and month

14. Time

15. Money amounts

16. Telephone numbers
Telephone numbers of the form 999-9999 or 999-999-9999 or 9-999-999-9999 where each 9 represents a digit.

Write a regular expression to recognize these telephone numbers.

You might start with three different regular expressions.
999-9999 999-999-9999 9-999-999-9999


17. Getting started
One way to get started is to write regular expressions that match each desired case.
         \d{3}-\d{4}    \d{3}-\d{3}-\d{4} \d{1}-\d{3}-\d{3}-\d{4}


18. Grouping parentheses
Assume that we want to recover each group, so add parentheses as follows.
                (\d{3})-(\d{4})         (\d{3})-(\d{3})-(\d{4}) (\d{1})-(\d{3})-(\d{3})-(\d{4})


19. Common part
The following is the common part on the right.
(\d{3})-(\d{4})

Add an optional part on the left as follows.
(.*)?(\d{3})-(\d{4})


20. More detail
The match for (.*)? on the left needs to be completed.

This method is similar to the Towers of Hanoi problem in that a smaller problem is being solved. This can be done as follows.
((.*)?-((\d{3})-))?((\d{3})-(\d{4}))


21. Complete it
Now complete the most innermost part.
((\d{1})?-((\d{3})-))?((\d{3})-(\d{4}))

Now test it with the regular expression tester. If you have trouble, try creating a test program and experiment with various regular expressions until you get something that works (see the following discussion on how you might do this).

To see a regular expression tester, see Regular expressions: Tester . Write a JavaScript regular expression to recognize the following pattern. omitted.

22. Exam help

23. End of page

by RS  admin@robinsnyder.com : 1024 x 640