Intro

A regular expression is a pattern or sequence of characters that has regular characters and meta-characters. The pattern serves as a template to find (and possibly replace) a desired arrangement in a body of text.

The VBScript RegExp object implements regular expression in a slightly different way than does the JavaScript/JScript RegExp object. Both of these RegExp objects are modeled after regular expressions in PERL. See my section about Regular Expressions.

Here is the syntax for regular expressions in JavaScript.

var objRegularExpression = /pattern/[switch];
var objRegularExpression = new regExp("pattern"[, "switch"]);

pattern contains a regular expression pattern. switch has three possibilities: i, g, or gi (where i is ignore case and g is global search for all occurrences of the pattern provided.

Properties

Methods

Regular expressions are often used in conjunction with the following methods of String objects. In these methods, the RegExp parameter can either a RegExp object or a literal string of a regular expression pattern.

EGs

Checks for proper email format

function FblnEMailFormat(str){
    var re = /^\w[-\.\w]+@\w+(\.\w+)+$/;
    if (re.test(str) || str=="") return true;
    return false;
}

function JSEG(obj) {
    alert ("Proper email format: " + FblnEMailFormat ("gh@bs"));
}

Changes all 's to ''s

This is particularly useful for entering values into fields via SQL code.

var strLName = new String(form.LName.value);
form.hidLName.value=strLName.replace(/'/g, "''");
//read from the hidLName field instead of LName.

Switch First Two Words

The following returns "Hernandez George":

str = "George Hernandez";
newstr = str.replace(/(\w+)\s(\w+)/, "$2, $1");

2004-03-21 08:27:19Z