In order to match and group all the fragments of URL, and all complex situations of different URL variations, our Reg Ex is a little-bit longer (324 chars), but it captures and groups all parts nicely (tested it in PHP and Java Script). Here is Twitter like way of doing it: var shorten Url = function(url,protocol,host,port,path,filename,ext,query,fragment) ; // our URL Reg Rx var url Regex = /\(?
I have applied this in one of my projects and its working there. ([a-z A-Z0-9] (\.[a-z A-Z0-9] ) .*)$ Let me know if you face any problem.
Detecting URLs in text strings is something that you will probably need sometime when parsing some text in your applications, and their shortening can also be quite handy (like Twitter does for example).
At first it may look like bunch of random characters, but actually it’s a pretty useful technique for both simple and complex matching of whatever you need within text string.
It is supported by all major programming languages (PHP, Perl, Java Script, Java, . Here you can see URL anathomy (along with good SEO practices), or just search across the web for it.
For example, you no longer have to worry about long links messing up your text.
Now here comes the fun part, once we’ve matched an URL, we can do with it whatever we like. Matching URLs is not so difficult, but matching them and grouping all URL fragments is. So, here is one URL Regular Expression to rule them all. Someone once said, Reg Ex is like a language of its own. I just couldn’t get myself to write this right now, but I will if someone is interested. Something like this (in Java Script): If you really want to know how, or have some uncertainties, just write below in comments. You can see it in action, and also test your own URLs: Test Reg Ex There is just one little catch.