Saturday, September 3, 2016

Regex simplification tool

Regex strings are useful when validating form inputs-- making sure people enter in their names and phone numbers correctly when filling out forms. However, in its existing syntax, it can be quite a headache to implement. I confess that when I need a regex string, I just google whatever I'm looking for, copy, paste, and if it works I forget about it.

Here's a typical regex string:

/^(?:[0-9]|[a-z]|[\._%\+-])+(?:@)(?:[0-9]|[a-z]|[\.-])+(?:\.)[a-z]{2,}$/i

Not easily readable, right? Here's where SRL (Simple Regex Language) wants to save you time. This tool uses more human-friendly syntax to generate the regex string for you. You can use their online generator, or install their tool for your site.

Here is one example of an SRL string, which is quicker to compute:

begin with any of (digit, letter, one of "._%+-") once or more,
literally "@",
any of (digit, letter, one of ".-") once or more,
literally ".",
letter at least 2 times,
must end, case insensitive


I haven't tried it out yet, but next time I need a regex I will!

No comments:

Post a Comment