/.../) and reports an error. One can use either the
RegExp() constructor or the
eval() function instead. We'll use the first one.
First, take a look at our solution, which includes validation for older browsers that do not support regular expressions:
This form's HTML is the same as the previous one, except that it doesn't generate an error on older browsers, and also demonstrates an alternative verification procedure. Here's the script (and the form):
Before dealing with regular expressions, the function must check if they are supported by the browser:
if (window.RegExp) ...
window. prefix is required because the
if statement cannot evaluate an object or property whose parent object is not explicitly specified (don't ask us why).
The function then assigns the regular expressions, as strings, to local variables. Notice that all backslashes must be escaped with a backslash (
\\), so these characters remain backslashes in the string. If you do not escape these backslashes, they are evaluated as metacharacters with the character that follows. The
RegExp() constructor requires its argument to be a regular expression, with the original backslashes in place. The string
"\d", for example, is actually the same as
"d", but in a regular expression these are not the same.
The next step is to create two regular expressions based on the above strings. In this script, the
RegExp() method is used to construct the regexps. The rest of the block is identical to the script in the previous section of this column.
indexOf() method to make sure the string has an "at" sign (
Created: October 23, 1997, 1997
Revised: December 4, 1997