- We design and build extraordinary applications for companies looking to make the next great idea a reality.
- learn more
Developer’s Notebook: Mastering (your fear of) regular expressions
Regular expressions are one of the most difficult programming concepts for novices and journeymen to wrap their heads around. Take a look at most any blog posting about RegExes and the comments will invariably be littered with words like "hate," "pain" and "AAAAARGH!"
Once you get comfortable with them, though, regular expressions become one of the most powerful tools in your programming arsenal. For Ajax/JavaScript developers, they can lend power and elegance to everything from form validators to keystroke interpreters to JSON, CSS and DOM parsers - in short, many of the thing you'll want to do on the client side of any powerful webapp. Take a look under the hood of any respected Ajax/DHTML library and you'll see RegEx literals being used liberally.
It's no secret that I'm big on programming books, especially O'Reilly ones, but I can think of few books that have been more useful than Jeffrey E. F. Friedl's "Mastering Regular Expressions". One difficult aspect of JavaScript regular expressions is their syntax, which is completely different from the better-known Perl variety. Friedl steps back from the implementation details of individual RegEx engines to explain the central concepts common to them all. After having this book for a year, I still refer to it so often that I can't say when I'll be ready to graduate to Tony Stubblebine's "Regular Expression Pocket Reference." In the meantime, when I'm away from my copy of the Friedl book, there are plenty of online resources to guide me.
Cheat sheets & quick references
These links don't offer really in-depth tutorials, but they do show you the JavaScript RegEx syntax at a glance.
- WikiCodia reference
- Visibone cheat sheet
- John Robert Morris cheat sheet
- JavaScript Kit tutorial
- Zytrax.com RegEx user guide
Interactive terminals
Apparently, everybody and their mother decided to build a little DHTML/Ajax app to let you create regular expressions, run arbitrary text against them, and check out the results. This is a fantastic way to play with the technology and get more confident in your abilities. Here are 9 different implementations of the same basic idea. I haven't used all of them, so let me know in the comments which are most useful.
- http://regexpal.com/
- http://www.rexv.org/
- http://www.xaprb.com/demos/rx-toolkit/
- http://www.cuneytyilmaz.com/prog/jrx/
- http://www.codeproject.com/jscript/regex2.asp
- http://lawrence.ecorp.net/inet/samples/regexp-format.php
- http://tools.netshiftmedia.com/regexlibrary/
- http://www.arachnoid.com/regex_lab/index.html
- http://weitz.de/regex-coach/
And for the over-achievers
For those of you so advanced in your RegEx powers that you've hit the limitations of the built-in JavaScript implementation, check out XRegExp, an open-source regular-expression library that supports named capture and other advanced features.
Technorati tags
Topics: Developer's Notebook, Javascript, Javascript Libraries, Tools, Tutorials
Comments: 3 so far
Leave a comment
About Pathfinder
Recent
- Making GWT JSON not Quite so Painful
- IDEA - preconference workshop 06 Oct 08
- HTML5, Ajax history management, and The Ajax Experience 2008 Boston
- A Look Back At Past Posts
- Flash Player on iPhone gossip
- Microsoft to Jump on Board EC2
- TAE Boston 2008: The Unsexy Presentations
- The Ajax Experience 2008: Hope to see you in Beantown
- TankEngine: New plugin for Rails iPhone Development
- Symphony of Ruby on Rails and Flex through RubyAMF
Archives
- October 2008
- September 2008
- August 2008
- July 2008
- June 2008
- May 2008
- April 2008
- March 2008
- February 2008
- January 2008
- December 2007
- November 2007
- October 2007
- September 2007
- August 2007
- July 2007
- June 2007
- May 2007
- April 2007
- March 2007
- February 2007
- January 2007
- December 2006
- November 2006
- October 2006
- September 2006
- August 2006
- July 2006
- June 2006
- May 2006
- April 2006
- March 2006


Hey, thanks for the XRegExp / RegexPal mentions! I’ve read both books you mentioned from cover to cover, and as a result I would strongly recommend against “graduating” from Friedl’s classic “Mastering Regular Expressions” to the pocket reference. The pocket reference tries to cover too many flavors in a tiny amount of space, so it ends up not really providing much information about any of them. For JavaScript-flavor regexes specifically (which, unfortunately, Friedl’s book doesn’t deal with), it covers it incompletely and quite poorly overall.
Comment by Steven Levithan, Friday, September 28, 2007 @ 12:23 pm
Thank you! Especially for the interactive testing sites.
Comment by Caustic Dave, Sunday, September 30, 2007 @ 8:03 pm
it’s good to read this post!Interested in JS RegExp.
Comment by frank, Tuesday, June 3, 2008 @ 10:13 am