|
||
| Inside Technique : Easy Cross-Browser Form Validation Using Regular Expressions : The Regular Expression Object The Regular Expression ObjectThe Regular Expression object exposes three methods and two properties. This is the object that performs the work of pattern-matching. The following are tables describing the methods and properties of the regular expression object. Methods
Properties
The RegExp ObjectThe RegExp object exposes four properties and no methods. This is the parent to the regular expression object. It is used to instantiate the regular expression object with its constructor and also to store information about its children’s pattern match searches. The RegExp object cannot be created directly, but it is always available. The RegExp object’s properties remain undefined until a child regular expression object successfully performs a search. The following is a table describing the properties of the RegExp object. Properties
Other Uses For Regular ExpressionsAlthough you can use regular expressions to test a string for validity against a search pattern, there are other uses for them. The String object has four methods that take regular expressions as arguments. Although most of the String methods parallel the methods of the Regular Expression object, the most useful one by far is the replace() method. You can use the replace() method to reformat a string. This is accomplished by using the $1…$9 properties of the RegExp object. Those properties are populated with the contents of the portions of the searched string that matched the portions of the search pattern contained within parentheses. The following example illustrates how to use the replace method to swap the order of first and last names and insert a comma and a space in between them: <SCRIPT LANGUAGE=”JavaScript 1.2”> var objRegExp = /(\w+)\s(\w+)/; var strFullName = ‘Jane Doe’; var strReverseName = strReverseName.replace(objRegExp, ‘$2, $1’); document.write(strReverseName) </SCRIPT> The output of this code will be “Doe, Jane”. How this works is that the pattern in the first parentheses matches “Jane” and this string is placed in the RegExp.$1 property. The \s (space) character match is not saved to the RegExp object because it is not in parentheses. The pattern in the second set of parentheses matches “Doe” and is saved to the RegExp.$2 property. The String replace() method takes the Regular Expression object as its first argument and the replacement text as the second argument. The $2 and $1 in the replacement text are substitution variables that will substitute the contents of RegExp.$2 and RegExp.$1 in the result string. You can also use replace() method to strip unwanted characters from a string before testing the string for validity or before saving the string to a database. It can be used to add formatting characters for the display of a string as well. The following is a table listing the methods of the String object that use regular expressions. String Methods Using Regular Expressions
Page 1:Easy Cross-Browser Form Validation Using Regular Expressions © 1997-2000 InsideDHTML.com, LLC. All rights reserved. |