SiteExperts.com Logo Home | Community | Developer's Paradise | Jobs
User Groups | Site Tools | Site Information | Search

Inside Technique : Easy Cross-Browser Form Validation Using Regular Expressions : The Regular Expression Object

The Regular Expression Object

The 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

MethodDescription
test(string)

Tests a string for pattern matches. This method returns a Boolean that indicates whether or not the specified pattern exists within the searched string. This is the most commonly used method for validation. It updates some of the properties of the parent RegExp object following a successful search.

exec(string)

Executes a search for a pattern within a string. If the pattern is not found, exec() returns a null value. If it finds one or more matches it returns an array of the match results. It also updates some of the properties of the parent RegExp object.

compile(pattern)

Compiles a regular expression pattern. When a regular expression is instantiated, the initial pattern is automatically compiled. Resetting the value of the pattern is slow. To speed up execution of your scripts it is recommended that you use the compile method if you plan to repeatedly update the pattern (such as in a loop).

Properties

PropertyDescription
source

Stores a copy of the regular expression pattern.

lastIndex

The index from which to begin the next search.

The RegExp Object

The 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

PropertyDescription
$n

n represents a number from 1 to 9
Stores the nine most recently memorized portions of a parenthesized match pattern. For example, if the pattern used by a regular expression for the last match was /(Hello)(\s+)(world)/ and the string being searched was “Hello world” the contents of RegExp.$2 would be all of the space characters between “Hello” and “world”.

index

Stores the beginning character position of the first successful match found in the searched string.

input

Stores the string against which a search was performed.

lastIndex

Stores the beginning character position of the last successful match found in the searched string. If no match was found, the lastIndex property is set to –1.

Other Uses For Regular Expressions

Although 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

MethodDescription
match( regular expression )

Returns, as an array, the results of search using the supplied Regular Expression object. Similar to the exec() method of the Regular Expression object. Also updates the $1…$9 properties in the RegExp object.

replace( regular expression, replacement text )

Returns a copy of the string with text replaced with the specified RegExp $1…$9 properties.

split ( string literal or regular expression )

Returns the array of strings that results when a string is separated into substrings. Splitting is done based on the occurrences of the string literal or regular expression matches.

search( regular expression )

Returns the position of first substring match in a regular expression search. Similar to reading the index property of the RegExp object after executing the exec() or test() methods.


Next - Sample Usage

Page 1:Easy Cross-Browser Form Validation Using Regular Expressions
Page 2:The Regular Expression Object
Page 3:Sample Usage
Page 4:Demonstration