actionscript-3apache-flexflex3

How to write a regular expression for URLs


I've been using the Regular Expression Explorer but I still can't come up with the right pattern.

Here's my URL:

http://pie.crust.com:18000/TEST/TEST.html

Here's my RegExp:

/[^http:\/\/][\w-\W]+[\/]/

And the output is:

ie.crust.com:18000/TEST/

All I want is the domain (basically everything inbetween // and /):

pie.crust.com:18000

What am I missing? I just can't figure it out. Any ideas?

Thank you in advance.


Solution

  • The part [^http:\/\/] is the same as [^htp:\/] and just enumerates all the characters which shouldn't be in the start part of the resulting string. So for http://pie.crust.com:18000/TEST/TEST.html http://p matches this enumeration. I suggest you the following expression:

    /http:\/\/([^\/]+)\/.*/
    

    You can use String.replace() the following way:

    var myUrl:String = "http://pie.crust.com:18000/TEST/TEST.html";
    var refinedUrl:String = myUrl.replace(/http:\/\/([^\/]+)\/.*/, "$1");