I had so much fun playing with the regular expressions for this one, that I ended up building a fairly elaborate testbed and missed my usual morning-cup-of-tea posting deadline. The page demonstrates using REs to pull out phone numbers, emails, urls, dates, times and prices from unstructured text, and uses a JS library I’m making freely available to search and replace within an HTML document. There’s some sample text to show it off, and you can put it through its paces by inputting your own strings to really test it.
If you want to see the power of this approach, try grabbing some of your own emails and pasting them into the custom box (it’s all client-side so I’ll never see any of them). You’ll be surprised at how much these expressions will pick up. Imagine how much useful information you could get from a whole company’s mailstore.
Here’s the exact REs I’m using, you may need to escape the back-slashes depending on your language:
Phone numbers (10 digit US format, with any separators)
URLs with protocols
Naked URLs (this will also pick up non-URLs like document.body)
These handle the common cases I’m interested in at the moment. There’s no end to how elaborate you could make them to handle all the possible different formats, but these cover a lot of ground. Now I have to resist the temptation to build these into a Firefox extension. IE’s RE engine does seem to want to overmatch with the email expression, sometimes pulling in characters past the end, but that seems to be an implementation quirk since I don’t notice that in the other environments like Firefox, Safari or grep.
Great post. I love what you’re doing.
I took your email regexp and modified it to make it case-insensitive:
Thanks Josh, that’s a very good point. I forgot because I’m testing all of these REs with the i modifier to make them case-insensitive, but it’s better to specify them the way you do.