JavaScript Regular Expression Tester

I have been working on a couple samples recently where I have become obsessed with the power of regular expressions.  There are plenty of areas in forms development where regular expressions can come in handy.  Maybe you want to validate that the user entered a syntactically-correct email address.  Maybe you want to change "LastName, FirstName" into "FirstName LastName".  The more I look at regular expressions, the more uses I find for them.  However, there are a couple of problems with my obsession (besides the danger that I’m on a path to OCD):

  1. Regular expressions easily become complex.  You start out doing a simple search/replace and it seems seductively easy.  A basic regular expression used in String.replace() means you can do in one line of code what would have otherwise required 20 lines of fragile script.  Then next thing you know you you’re processing HTML blocks.  You’re struggling to capture a repeating group — it *almost* works — "just another five minutes and I’ll get it"…
  2. Good news: There is plenty of online documentation and plenty of online tools to test regular expressions and feed your habit.  There are also plenty of forums populated with questions from regular expression addicts.  Bad News: There is a frustrating lack of uniformity in the regular expression functionality in various development environments — whether it’s Perl, JavaScript, Java, grep, Python, Flex …

Eventually my frustration/obsession grew to the point where I built my own RegEx testing form.  That way I could be certain that the expressions I tested would work with the version of JavaScript found in the version of Reader I was targeting.

The form tests regular expressions in two contexts: RegExp.exec() and String.replace().

I won’t try to explain the usefulness of both these methods, since any modest JavaScript reference would do a much better job.  The test form has an input fields where you enter the source string, regular expression and (optional) replacement string.  There are check boxes to control the RegExp attributes.  When you click the "find" button, the expression is evaluated (RexExp.exec()) and the results are dumped into a series of repeating subforms.  If you choose the replace option, the script uses the regular expression with String.replace().

Enjoy.  But proceed with caution.  I’m not responsible for any social disorders you develop along the way.  However, I might be forming a support group…

3 Responses to JavaScript Regular Expression Tester

  1. Very cool. Great idea to create a PDF implementation of this. While there are several installable applications and online testers, I really dig the no-install, offline portability of this one. Very useful. Thanks for sharing.

  2. Sven Pelta says:

    John,from one RegEx addict to another: try RegexBuddy. It keeps you almost sane… ;)Among other cool stuff, it solves the problem you have with various Regex flavours.

  3. Trevor, Sven:Thanks for the encouragement. I’m happy to know that I’m suffering in good company.John