c00000fd
c00000fd

Reputation: 22307

Single regexp to get page URL but exclude port number from a full URL

I'm trying to come up with a regexp to get the page URL from the full URL but exclude a possible port number from it. So far I came up with the following JS:

var res = url.match(/^.*\:\/\/(?:www2?.)?([^?#]+)/i);
if(res)
{
    var pageURL = res[1];
    console.log(pageURL);
}

If I call it for this:

var url = "http://www.example.com/php/page.php?what=sw#print";

I get the correct answer: example.com/php/page.php

But if I do:

var url = "http://www.example.com:80/php/page.php?what=sw#print";

I need it to return example.com/php/page.php instead of example.com:80/php/page.php.

I can remove it with the second regexp, but I was curious if I could do it with just one (for speed)?

Upvotes: 1

Views: 524

Answers (6)

Avinash Raj
Avinash Raj

Reputation: 174826

You could use replace method to modify your original string or Url,

> var url = "http://www.example.com/php/page.php?what=sw#print";
undefined
> var url1 = "http://www.example.com:80/php/page.php?what=sw#print";
undefined
> url.replace(/^.*?:\/\/(?:www2?.)?([^/:]+)(?::\d+)?([^?#]+).*$/g, "$1$2")
'example.com/php/page.php'
> url1.replace(/^.*?:\/\/(?:www2?.)?([^/:]+)(?::\d+)?([^?#]+).*$/g, "$1$2")
'example.com/php/page.php'

DEMO

Upvotes: 0

guest271314
guest271314

Reputation: 1

Try

var url = "http://www.example.com:80/php/page.php?what=sw#print";
var res = url.split(/\w+:\/\/+\w+\.|:+\d+|\?.*/).join("");

var url = "http://www.example.com:80/php/page.php?what=sw#print";
var res = url.split(/\w+:\/\/+\w+\.|:+\d+|\?.*/).join("");
document.body.innerText = res;

Upvotes: 0

anubhava
anubhava

Reputation: 785866

You can modify your regex to this:

/^.*\:\/\/(?:www2?.)?([^/:]+)(?:[^:]*:\d+)?([^?#]+)/i

RegEx Demo

It will return 2 matches:

1: example.com
2: /php/page.php

as match[1] and match[2] respectively for both inputs that you can concatenate.

http://www.example.com/php/page.php?what=sw#print

OR

http://www.example.com:80/php/page.php?what=sw#print

Update: Here are performance results on jsperf.com that shows regex method is fastest is of all.

Upvotes: 3

ncardeli
ncardeli

Reputation: 3492

How about a group for matching the port, if present?

var url = "http://www.example.com:80/php/page.php?what=sw#print";
var res = url.match(/^.*\:\/\/(?:www2?.)?([^?#\/:]+)(\:\d+)?(\/[^?#]+)/i);
if(res)
{
    var pageURL = res[1]+res[3];
    console.log(res, pageURL);
}

Upvotes: 0

conceptdeluxe
conceptdeluxe

Reputation: 3903

Why would you use a regex at all?


EDIT:

As pointed out by @c00000fd: Because document might not be available and document.createElement is very slow compared to RegExp - see:

http://jsperf.com/url-parsing/5

http://jsperf.com/hostname-from-url

Nevertheless I will leave my original answer for reference.


ORIGINAL ANSWER:

Instead you could just use the Anchor element:

Fiddle:

http://jsfiddle.net/12qjqx7n/

JS:

var url = 'http://foo:[email protected]:8080/php/page.php?what=sw#print'
var a = document.createElement('a');
a.href = url;

console.log(a.hash);
console.log(a.host);
console.log(a.hostname);
console.log(a.origin);
console.log(a.password);
console.log(a.pathname);
console.log(a.port);
console.log(a.protocol);
console.log(a.search);
console.log(a.username);

Additional information:

http://www.w3schools.com/jsref/dom_obj_anchor.asp

Upvotes: 0

max
max

Reputation: 102250

Keep it simple:

~ node 
> "http://www.example.com:3000/php/page.php?what=sw#print".replace(/:\d+/, '');
'http://www.example.com/php/page.php?what=sw#print'
> "http://www.example.com/php/page.php?what=sw#print".replace(/:\d+/, '');
'http://www.example.com/php/page.php?what=sw#print'

Upvotes: 0

Related Questions