Regular Expression Object

An object that contains a regular expression pattern along with flags that identify how to apply the pattern.

// The explicit constructor for a Regular Expression object.
function RegExp(pattern : String [,flags : String])
function RegExp(regexObj : System.Text.RegularExpressions.Regex)

// The implicit constructor for a Regular Expression object.
/pattern/[flags]

pattern

Required. The regular expression pattern to use. If you use Syntax 1, the pattern must be a string. If you use Syntax 2, the pattern is delimited by the "/" characters.

flags

Optional. If you use Syntax 1, the flags must be in a string. If you use Syntax 2, the flags characters immediately follow the last "/" character. Available flags, which may be combined, are:

  • g (global search for all occurrences of pattern)

  • i (ignore case)

  • m (multiline search)

regexObj

Required. A Regex object that contains the regular expression pattern to use.

The Regular Expression object should not be confused with the global RegExp object. Although they sound similar, they are easily distinguishable. The properties of the Regular Expression object contain information about only one particular Regular Expression instance, while the properties of the global RegExp object contain continually updated information about each match as it occurs.

Regular Expression objects store patterns used to search strings for character combinations. After the Regular Expression object is created, it is either passed to a string method, or a string is passed a method of the Regular Expression object. Information about the most recent search performed is stored in the global RegExp object.

Use Syntax 1 when the search string changes frequently or is unknown, such as strings derived from user input. Use Syntax 2 when you know the search string ahead of time.

In JScript the pattern argument is compiled into an internal format before use. For Syntax 1, pattern is compiled just before use or when the compile method is called. For Syntax 2, pattern is compiled as the script loads.

NoteNote

The Regular Expression object interoperates with the .NET Framework System.Text.RegularExpressions.Regex data type within JScript. However, other Common Language Specification (CLS) languages cannot use the Regular Expression object because only JScript provides the object; it is not derived from a .NET Framework type. Consequently, when type-annotating the parameters and return types of CLS-compliant methods, make sure to use the System.Text.RegularExpressions.Regex data type instead of the Regular Expression object. However, you may use the Regular Expression object to type annotate identifiers other than the parameters or return types. For more information, see Writing CLS-Compliant Code.

The following example illustrates the use of the Regular Expression object. Objects re1 and re2 are created and contain regular expression patterns with the associated flags. In this case, the resulting Regular Expression objects are then used by the match method:

var s : String = "The quick brown fox jumps over the lazy dog";
// Create regular expression object by using Syntax 1.
var re1 : RegExp = new RegExp("quick","i");
// Create regular expression object by using Syntax 2.
var re2 : RegExp = /THE/i;

// Find a match within string s.
print(s.match(re1));
print(s.match(re2));

The output from this script is

quick
The
Was this page helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft