Hacker News new | ask | show | jobs
by evantish 4373 days ago
> Why use regex? It's much simpler to write a URL validator by hand...

I actually have a use-case. I am firming up a feature right now that detects when a user types a url into a text field and replaces it on the fly with a footnote-style reference number (much like your comment above). This is done to (1) minimize input string length, (2) draw the benefits of a consistent interface, and (3) avoid screwing around with the fragility of url shortening nonsense.

I may regret this, but here's a link to my dev environment for this feature (please be gentle), to see it in action:

https://cloudcity.tenfourgood.com/cloudcity

...just start typing in the big text box and add in some urls.

It uses a fairly ugly-looking regular expression[0].

If you take out the unicode mumbojumbo, it's not really THAT tricky of a pattern. It does fail on IP addresses and it may be a little over-aggressive on matching, but, I wanted it to catch things like "abc.com" and "//xyz.com".

Edit: Formatting and clarity. Removed the explicit regular expression because it predictably got garbled.

[0] http://regexr.com/38vsq not exactly the same one I'm currently using, but it's pretty close. See source for most up-to-date version.