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.
.$_, aka .input or RegExp["$_"]. Read-only. Returns the string that the RegExp is matched against, i.e. the target string..$*, aka .multiline. Whether the pattern matches before or after "\n" or "\r"(True), or not (False)..$&, aka .lastMatch. Read-only. The last matched characters..$+, aka .lastParen. Read-only. The last parenthesized (captured) substring match, if any. .$`, aka .leftContext. Read-only. The substring preceding the most recent match..$', aka .rightContext. Read-only. The substring following the most recent match..$1, ..., .$9. Read-only. Returns the last nine parenthesized (captured) sub-strings in a match. Since RegExp is a global object, you can access RegExp.$1, RegExp.$2, and so forth, from anywhere in the script..constructor. See object.contstuctor..global. Read-only. Whether or not the g switch is used..ignoreCase. Read-only. Whether or not the i switch is used..index. Read-only. Integer indicating the 1st successful match. Not available for NN4..lastIndex. Read or write. Integer indicating the start of the next match. Available only if the g switch is used. Not available for NN4..prototype. See object.prototype..source. Read-only. The pattern of the RegExp..compile(pattern[, switch]). Compiles a RegExp object. Use if pattern and switch will remain the same for multiple uses of the RegExp object. Also use compile() to change the pattern or modifier of a RegExp object..exec(TargetString). Searches for a match and returns a result array. This is the default method for RegExp objects so RegExp(TargetString) will work as well. If you are executing a match simply to find true or false, use test() or the String search() instead. No match returns null, otherwise the resulting array has these properties:
index. The zero-based index of the match in the string.input. The original string.[0]. The portion of the string that was matched last.[1], [2], ..., [n]. The parenthesized substring matches, if such exist.lastIndex. The index at which to start the next match.ignoreCase. Indicates if the i modifier was used.global. Indicates if the g modifier was used.source. The pattern's text itself..test(TargetString). Searches for a match and returns true or false . Comparable to the String search() method..toSource(). See object.toSource() ..toString(). See object.toString(). Usually returns pattern ..valueOf(). See object.valueOf().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.
.match(RegExp). Operates just like the RegExp exec() method except that it returns an array of the matches. .replace(RegExp, ReplacingString) or replace(RegExp, function). Operates just like the PERL s///e (substitute) operator. Returns a new array..search(RegExp). Operates just like the RegExp test() method, but instead of returning true or false, it return the index of the match or -1..split(RegExp). Tries to split the string into substrings according to the RegExp. If the pattern is not found, then split() returns the original string. If the pattern is found, an array is returned.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"));
}
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.
The following returns "Hernandez George":
str = "George Hernandez"; newstr = str.replace(/(\w+)\s(\w+)/, "$2, $1");
2004-03-21 08:27:19Z