fumeng
fumeng

Reputation: 1820

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.

Upvotes: 1

Views: 134

Answers (5)

Shekhar
Shekhar

Reputation: 151

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

Upvotes: 0

Constantiner
Constantiner

Reputation: 14221

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");

Upvotes: 2

shanethehat
shanethehat

Reputation: 15570

(?<=http:\/\/)[a-zA-Z.:0-9-]+

The p of "pie" is being matched as part of the http rule, and so is not included. Using a positive look-behind fixed this.

http://regexr.com?2uhjf

Upvotes: 1

Doug Kress
Doug Kress

Reputation: 3537

Try this:

@http://+(.*?)/@

(Your regexp doesn't have to start and end with / - it's easier to use something else that isn't in your search string.

Upvotes: 1

Eder
Eder

Reputation: 1884

Try this one: http:\/\/([^\/]+)

Upvotes: 3

Related Questions