OBJECT:
RegExp
new
RegExp("pattern"[, "flags"])
The RegExp object contains the pattern of a regular expression,
and is used to match strings using its methods and properties. The predefined
RegExp object has static properties set whenever a regular expression
is used, as well as user-defined ones for individual objects. A RegExp
object can be created in two ways: either by using the constructor
function, or with a literal text format. In both cases you need to specify
the text pattern of the regular expression, and optionally one of the
three possible flags: 'g' for a global match, 'i' to ignore case, or
'gi' for a case-insensitive global match.
The following code creates a regular expression using the constructor
function, that matches an initial letter 'a' irrespective of case:
Code:
myRegExp = new RegExp("^a", "i")
Note that when using the constructor function, you must use
quotation marks to indicate strings. Also, the normal string escape
rules apply and you must use a back slash before special characters
as in the following code which creates a regular expression of a tab
character:
Code:
myRegExp = new RegExp("\\t")
Using the literal text format you do not need to use quotation marks.
To create a regular expression consisting of a case-insensitve initial
'a' you could use the following code:
Code:
myRegExp = /^a/i
...and a regular expression consisting of a tab character could be
created as follows:
Code:
myRegExp = /\t/
The literal notation of a regular expression provides compilation
of it when the expression is evaluated, and this is used when you know
that a regular expression is going to remain constant. When you know
that an expression is going to change, or you don't know what that expression
is because it will be input by a user, you need to use the constructor
function which is compiled at run time. You can, however, compile a
regular expression at any time using the compile method. Each
window has its own predefined RegExp object thus ensuring that
different threads of JavaScript execution don't overwrite values of
the RegExp object.
For a complete list and description of the special characters that
can be used in a regular expression, see the special
characters page.
PROPERTIES
$1, ..., $9 Property
These are properties containing parenthized substrings (if any) from
a regular expression.
$ Property
See the input property.
$* Property
See the multiline property.
$& Property
See the lastMatch property.
$+ Property
See the lastParen property.
$` Property
See the leftContext property.
$' Property
See the rightContext property.
constructor Property
This property specifies the function that creates an object's prototype.
See the Object.constructor property.
Syntax: RegExp.constructor
global Property
This property reflects whether the 'g' flag was used to match a regular
expression globally in a string, or just the first occurrence of it.
Its value is true if the 'g' flag was used and false if
not. Note that this property is read-only but that calling the compile
method does alter it.
Syntax: object.global
ignoreCase Property
This property reflects whether the 'i' flag was used for a case-insensitive
match of a regular expression in a string, returning true if
it was and false if not. Note that this property is read-only
but that calling the compile method does alter it.
Syntax: object.ignoreCase
input Property
This property is a string against which a regular expression is matched.
Syntax: RegExp.input
lastIndex Property
This property is an integer that specifies the index at which to start
the next match, but is only set if the regular expression uses the 'g'
flag to specify a global search.
Syntax: object.lastIndex
lastMatch Property
This property is the last matched characters. As this property is static,
you always use RegExp.lastMatch.
Syntax: RegExp.lastMatch
lastParen Property
This property contains the last matched parenthesized substring (if
any), and as a static property is always refered to using RegExp.lastParen.
Syntax: RegExp.lastParen
leftContext
Property
This property is the substring upto the character most recently matched;
i.e. everything that comes before it, and as a static property, is always
used as RegExp.leftContext.
Syntax: RegExp.leftContext
multiline Property
This property reflects whether a search is to be carried out over multiple
lines, returning true if it is, and false if not. Being
a static property, you always use RegExp.multiline. When an event
handler is called for a TEXTAREA form element, the browser sets the
multiline property to true. Once the event handler has
finished executing, it is reset to false, even if it was set
at true before the event handler was called.
Syntax: RegExp.multiline
prototype Property
This property represents the prototype for this class, and allows you
to add your own properties and methods to all instances of it. See the
Function.prototype
property.
Syntax: RegExp.prototype
rightContext
Property
This property is the substring after the character most recently matched;
i.e. everything that follows it, and as a static property, is always
used as RegExp.rightContext.
Syntax: RegExp.rightContext
source Property
This is a read-only property containing the source of the regular expression:
i.e. everything except the forward slashes and any flags. The source
property cannot be changed directly, however calling the compile
method does alter it. For example, with the regular expression
rexp = /[^aeiou\s]{2}/g the value of the source property
would be [^aeiou\s]{2}.
Syntax: object.source
METHODS
compile Method
This method compiles a regular expression object during execution of
a script.
Syntax: object.compile(pattern[, flags])
exec Method
This method executes a search for a match in a specified string, returning
a result array.
Syntax: object.exec([str])
object([str])
test Method
This method tests for a match of a regular expression in a string, returning
true if successful, and false if not.
Syntax: object.test([str])
toSource Method
This method returns the source code of a RegExp object and is
usually called internally by JavaScript. It also overrides the Object.toSource
method.
Syntax: object.toSource()
toString Method
This method returns a string representing the RegExp object,
and this overrides the Object.toString
method.
Syntax: object.toString()
valueOf Method
This method returns a primitive value for the RegExp object as
a string data type, and is equivalent to the RegExp.toString
method. It is usually called internally by JavaScript and overrides
the Object.valueOf method.
Syntax: object.valueOf()
NOTE:
The regExp object also inherits the watch
and unwatch methods from the
Object object.
|