Figure 1   Regular Expression Syntax Reference

Character Description
\ Marks the next character as special. /n/ matches the character "n". The sequence /\n/ matches a linefeed or newline character.
^ Matches the beginning of input or line.
$ Matches the end of input or line.
* Matches the preceding character zero or more times. /zo*/ matches either "z" or "zoo".
+ Matches the preceding character one or more times. /zo+/ matches "zoo" but not "z".
? Matches the preceding character zero or one time. /a?ve?/ matches the "ve" in "never".
. Matches any single character except a newline character.
(pattern) Matches pattern and remembers the match. The matched substring can be retrieved from the result Array object elements [1]...[n] or the RegExp object's $1...$9 properties. To match parentheses characters, (), use "\(" or "\)".
x|y Matches either x or y. /z|food?/ matches "zoo" or "food".
{n} n is a nonnegative integer. Matches exactly n times. /o{2}/ does not match the "o" in "Bob", but matches the first two o's in "foooood".
{n,} n is a nonnegative integer. Matches at least n times. /o{2,}/ does not match the "o" in "Bob" and matches all the "o's" in "foooood". /o{1,}/ is equivalent to /o+/.
{n,m} m and n are nonnegative integers. Matches at least n and at most m times. /o{1,3}/ matches the first three "o's" in "fooooood".
[xyz] A character set. Matches any one of the enclosed characters. /[abc]/ matches the "a" in "plain".
[^xyz] A negative character set. Matches any character not enclosed. /[^abc]/ matches the "p" in "plain".
\b Matches a word boundary, such as a space. /ea*r\b/ matches the "er" in "never early".
\B Matches a nonword boundary. /ea*r\B/ matches the "ear" in "never early".
\d Matches a digit character. Equivalent to [0-9].
\D Matches a nondigit character. Equivalent to [^0-9].
\f Matches a form-feed character.
\n Matches a linefeed character.
\r Matches a carriage return character.
\s Matches any white space including space, tab, form feed, and so on. Equivalent to [ \f\n\r\t\v].
\S Matches any nonwhite space character. Equivalent to [^ \f\n\r\t\v].
\t Matches a tab character.
\v Matches a vertical tab character.
\w Matches any word character including underscore. Equivalent to [A-Za-z0-9_].
\W Matches any nonword character. Equivalent to [^A-Za-z0-9_].
\num Matches num, where num is a positive integer. A reference back to remembered matches. \1 matches what is stored in RegExp.$1.
/n/ Matches n, where n is an octal, hexadecimal, or decimal escape value. Allows embedding of ASCII codes into regular expressions.


Figure 2   Object Methods

compile Compiles the regular expression into an internal format.
exec Executes a search for a match in a specified string. Returns an array of matches.
test Tests whether a pattern exists in a string. Returns true or false.


Figure 3   Object Properties

global Indicates whether the global switch is on, that is should it match all possible occurrences of pattern. Read Only.
ignoreCase Indicates whether the switch for ignoring case is on. Read Only.
lastIndex Specifies the position at which next match search should begin.
Source Contains the regular expression pattern string. Read Only.


Figure 4   RegExp Object Properties

Name Shorthand Description
$1...$9   Stores each of the nine possible remembered patterns matched.
index   Indicates where the first successful match begins in a searched string.
input $_ Contains the string the match was performed against. Read only.
lastIndex   Indicates where the last successful match ended in a string that was searched.
lastMatch $& Specifies the last matched characters. Read only.
lastParen $+ Contains the last matched parenthesised substring. Read only.
leftContext $` Returns the input string up to the point of the most recent match. Read only.
multiline $* Indicates whether the last match ran across line breaks. Read only.
rightContext $' Specifies the rest of the input string past the most recent match. Read only.
Note: RegExp object has no methods.


Figure 6   Quick.html

<a href="javascript:window.open('quick.html','QO', 'width=180,
height=200,toolbars=no,statusbar=no,scrollbar=no')">
 
 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
 <HTML>
 <HEAD>
 <TITLE>My own shop</TITLE>
 <META NAME="Generator" CONTENT="TextPad 3.0">
 <META NAME="Author" CONTENT="Heidi Housten">
 <META NAME="Keywords" CONTENT="Javascript, regexp">
 <META NAME="Description" 
   CONTENT="Demonstrates the use of regular expressions in JScript">
   
 <script language=javascript>
 
 function validate_form()
 {
     // Go through each object in the ValObjs array
     for (each_item in ValObjs)
     {
 
       // Send the name of this element to the field validation function
       if ( ! validate_field(each_item) )
       {
          // leave the loop if there has been an error
          // and cancel the submission
          return false
       }
     }
     
     // Passed all the tests, let the submit proceed
     return true
 }
 
 function validate_field(inobj)
 {
     // Get the form field
     FF = document.QOForm[inobj]
 
     // and corresponding Validation object
     CurrObj = ValObjs[inobj]
 
     // set up the RegExp object for the current object
     template = new RegExp( CurrObj.regexp,CurrObj.options )
     
     // and test the value - return true if there is a match
     if ( template.test(FF.value) )
     {
        return true
     }
     else
     {
         // Failed test set focus to field in question
         FF.focus()
         
         // and give corresponding error message to viewer
         alert ( CurrObj.message )
         
         return false
     }
 }
 
 function VO(regex,msg,opts)
 {
       this.regexp=regex
       this.message=msg
       this.options=opts
 
       return this
 }
 
 // We will create an array of objects with the regular expression,
 // the error message and options.
 // We are using the same name as each field uses in the form to make
 // it easier to access the form values when validating.
 
 ValObjs = new Array
 
 msg="Please enter your 6-8 digit account number"
 ValObjs["account"]= new VO("^\\d{6,8}$",msg,"")
 
 msg="Please enter the full part number like this:\n\n123456-5678"
 ValObjs["partno"] = new VO("^\\d{6}\-\\d{4}$",msg,"")
 
 msg="Please enter your email address\nor phone number "
 msg=msg +"in the following formats.\nme@my.com\nor\n222-5555"
 pattern="^[a-z\\.]+@[a-z\\.]+$|^\\d{3}\-\\d{4}$"
 ValObjs["emailaddr"]  = new VO(pattern,msg,"i")
 
 </script>
 <style>
 body,input { font-family:verdana;font-size:8pt }
 </style>
 </HEAD>
 <BODY BGCOLOR="#FFFFFF" TEXT="#000000">
 <b>Quick Order Form</b>
 <form action="javascript:alert('order okay')"
   method=post name="QOForm" onsubmit="return validate_form()">
 
 Account number<br>
 <input type=text value="" name="account"><br>
 Part number<br>
 <input type=text value="000000-0000" name="partno"><br>
 Email or phone<br>
 <input type=text value="" name="emailaddr"><br>
 <br>
 <center><input type=submit value="Order it!"></center>
 </form>
 </BODY>
 </HTML>