spacer
Yehuda Shiran February 9, 2000
Compiling Regular Expressions
Tips: February 2000

Yehuda Shiran, Ph.D.
Doc JavaScript

Developer News
News Flash: Adobe Has iPhone Workaround
Adobe's Flash 10.1 Goes Mobile (Minus iPhone)
A Salute to Visionary CEOs

You can boost operations with regular expressions by compiling them with the compile() method. Its syntax is:

reg.compile("PATTERN", ["g"|"i"|"gi"])

where reg is the name of a regular expression variable, and PATTERN is the text of the regular expression.

Use the compile() method with a regular expression variable that was created with the constructor function (not the literal notation). Use the compile() method when you know the regular expression will remain constant (after getting its pattern) and will be used repeatedly throughout the script. This method actually converts the specified pattern into its internal format, for faster execution.

The compile() method can also be used to change a regular expression during execution:

var reg = new RegExp("Bart", "i"); // reg matches Bart here
reg.compile("Lisa", "i") // reg does not match Bart here

You can also use the compile() method to modify a regular expression's modifier:

var reg = new RegExp("bart", "i"); // reg matches Bart here
reg.compile("bart"); // reg does not match Bart here

Learn more about regular expressions in Column 5, Regular Expressions.


People who read this tip also read these tips:

Look for similar tips by subject:

internet.commediabistro.comJusttechjobs.comGraphics.com

Search:

WebMediaBrands Corporate Info

Legal Notices, Licensing, Reprints, Permissions, Privacy Policy.
Advertise | Newsletters | Shopping | E-mail Offers | Freelance Jobs

webref The latest from WebReference.com Browse >
Building a Banking Application Home Page with OOP · Mixing Scripting Languages · Review: phpFox, a Social Networking CMS with all the Bells and Whistles
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
Enterprise 2.0: Social Networking in the Cloud · BroadSoft Marketplace Hastens Pace of Telephony Innovation · Review: HTC Hero for Sprint