Using Regular Expressions

by Jan Verhoeven, 1 November 2001

Regular expressions are a great tool for validating form input. Javascript had regular expressions (r.e.) from the beginning. Meanwhile r.e. are also available in VBScript.

This page uses javascript for the r.e.

Experimenting with regular expressions.

Below you can experiment with r.e. Enter a source text and a r.e. pattern in their boxes and click the validate button en the result will be displayed.

Source 
Pattern 
Result 
 

Sample expressions.

Try the various expressions discussed below and get a feel of what you can do with r.e. The patterns are described in plain language. You can copy and past the example patterns into the pattern textbox.

Integers

  ^\d+$

Match from the beginning of the string ^ to the end of the string $, digits only \d and one or more of them +

Dates

  ^([1-9]|1[0-2])/([1-9]|[12][0-9]|3[01])/\d{4}$

The pattern given is for matching a mm/dd/yyyy format. Their are many ways to enter a date and by instructing the user to use a certain format you can validate against that format.

A gain we use the begin of string ^ and end of string $ meta-characters. Then we take each of the sections in mm/dd/yyyy one by one. In the first part we are matching the month number which can be from 1 thru 12. In the pattern we say that either a character in the set [1-9] will match or | a number starting with 1 and followed by a character in the set [0-2].

Then we match the day dd part. Again we provide alternatives: either a character in the [1-9] set or a 1 or a 2 [12] followed by a character in the [0-9] set, or a 3 followed by a character in the [01] set by which we cover days 30 trhu 31.

Finally we match the year part with \d{4}, meaning a sequnce of exactly {4} digits \d.

Passwords

Suppose we want to allow passwords from 5 thru 16 characters and only having alphanumerics or the underscore _ character.

  ^\w{5,16}$

Again we match the whole string by using the ^ and $ meta-characters. Next we say we want to match alpanumerics ([a-zA-Z0-9]) for which we use the \w meta-character. And we add the constraint of 5 characters minimum and 16 characters maximum {5,16}

Not empty

Very often we only want to be sure that a field is not empy.

  ^.+$

We say any character . and one or more +