JavaScript RegExp Class



Navigation Aids -- This Page        Navigation Aids -- This Topic        Navigation Aids -- This Site








Quick Reference for the RegExp Class



The RegExp Class Reference Table
Native class
Inherits From Object
Constants Properties Methods Constructors
Syntax Arguments Returns
 
None Instance Properties
global
ignoreCase
lastIndex
multiline
source

Class Properties
index
input
lastMatch
lastParen
leftContext
rightContext
exec()
test()
toString()
new RegExp(pattern)
new RegExp(pattern, flags)
pattern
flags
A new RegExp Object




RegExp Class Description

JavaScript supports regular expressions through the RegExp class. RegExp objects are instantiated from the RegExp native class via the RegExp constructor. The constructor for the RegExp object can take one or two arguments. The first argument describes a pattern. The optional second argument describes flags that can specify additional regexp processing instructions (the Perl language term for flag is "modifiers").

A regular expression is an object that describes a pattern of characters. This pattern of characters is used to match against a target string and in there simplest form can look like a string. However, regular expressions have there own unique syntax. Regular expression patterns may contain meta characters that control the string matching process. And the matching process can range from the very simple to the very complex.

Instantiated objects of the RegExp class type will have access to two instance methods. These methods are test() and exec(). The String class also has methods that support RegExp objects.

JavaScript regular expressions can be a perplexing subject. The reader is advised to review our related pages on this subject.





RegExp Class Constants

There are no explicit constants for the class.





RegExp Class Properties

Typically, if a native class has related properties, they will be instance properties. The RegExp class is unique in that besides having instance properties, this class also has class (static) properties. First we will describe the RegExp instance properties and follow up with the RegExp class properties.





RegExp Class Instance Properties





RegExp Class Static Properties

Class (static) properties are rare in JavaScript. And before we discuss the RegExp class properties, we will review the behavior and characteristics of such properties.

Given the above overview of class properties, we are now ready to discuss the RegExp class properties. Each property is shown with an example. The target string and RegExp object for the illustrations is shown here. var sTarget = "it is a long, long way home"; var reObj = /(l)ong/g; reObj.test(sTarget); //the test() method has reset all class properties





RegExp Class Inherited Properties

The RegExp class does inherit the following properties from the base Object class.





RegExp Class Instance Methods

The RegExp class does inherit some methods from the base Object class.

The explicit method toString of the RegExp class will override the corresponding method of the base Object class.


The Methods for the RegExp Class
Method Syntax / Arguments Returns Description / Example
 
exec( ) Syntax:
reObj.exec(string)

Arguments: string
string: the target string
Returns an array of match results. The exec( ) method behaves as follows:
  • returns an array of matching results; this returned array is similar to the array returned from String.match(), however, exec() returns more info:
    • element zero of the array is the matched text from target string
    • subsequent element in the array relate to matched text for group sub expressions in the pattern; these elements are significant when pattern references are required
  • if there is no match, it returns null
  • this method resets all of the RegExp class properties to the current matching scenario
  • when the regexp object is specified with the global modifier:
    • it starts the search at the location specified by lastIndex property
    • if it finds a match it resets lastIndex to the position in the target string of the next after the match
    • always reset lastIndex to zero before using the global modifier with this method
  • the method will throw a TypeError exception when the method is invoked on an object that is not a RegExp

Example:
var sTarget = "it is a long way home"; var reObj = /(l)ong/; document.write(reObj.exec(sTarget)); //returns array: long,l //where element zero is match text, long //element one is first group match,letter l
test( ) Syntax:
reObj.test(string)

Arguments: string
string: the target string
Returns a boolean indicating results of match. The test( ) method behaves as follows:
  • returns "true" for a pattern match with target string and "false" when there is no match
  • this method resets all of the RegExp class properties to the current matching scenario
  • the method will throw a TypeError exception when the method is invoked on an object that is not a RegExp

Example:
var sTarget = "it is a long way home"; var reObj = /(l)ong/; document.write(reObj.test(sTarget)); //returns true
toString( ) Syntax:
reObj.toString( )

Arguments: None
Returns a string of a RegExp object. The toString( ) method behaves as follows:
  • returns a string version of the RegExp object
  • the returned value will be a string representation in the form of a regular expression literal
  • both the pattern and the optional flags are returned
  • the returned value may be implementation specific especially when the pattern contains escaped characters (the complete escape sequence notation may not be returned)

Example:
var reObj = /(?:19|20)\d{2}/g; document.write(reObj.toString()); // returns: /(?:19|20)\d{2}/g




RegExp Class Constructor





RegExp Class Constructor Example

var reDog = new RegExp("dog", "g"); // "dog" is the pattern and "g" is the global flag

Top            

Rx4AJAX        About Us | Topic Index | Contact Us | Privacy Policy | 2008 This Site Built By PPThompson