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

Yehuda Shiran, Ph.D.
Doc JavaScript

Developer News
Microsoft Shows Off Silverlight 4, IE9 Plans
Metasploit Expands Vulnerability Test Framework
HyperCard Reborn?

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, Permissions, Privacy Policy.
Advertise | Newsletters | Shopping | E-mail Offers | Freelance Jobs

webref The latest from WebReference.com Browse >
Rolling Out Your Own HTML Application Version Control · HTML 5: Client-side Storage · Working with Ajax Server Extensions
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
Wi-Fi Product Watch, November 2009 · Chip Market Recovering From '08 Collapse · Low-Cost Tools to Kickstart Your New Business