开发者

Function that explodes string?

With my current function:

http://piza.com/something returns pizza.com

How do I change it 开发者_如何学Goso it doesn't remove what's after / but keeps removing http, https and www?

E.G: http://piza.com/something/1 should return pizza.com/something/1

function cleanUrl(url) { 
    return url.replace(/^(http(s)?:\/\/)?(www\.)?([^\/]+)(\/.*)?$/gi,"$4");
}


Try before you ask. So many regex questions on stackoverflow are basically 'write my regex'.

Anyway, something like this should work (untested):

function cleanUrl(url) { 
    return url.replace(/^(http(s)?:\/\/)?(www\.)?/gi,"");
}


Here's something that will work.

return url.replace(/(http(s)?:\/\/)?(www\.)?/gi,"");


if (url.indexOf('http://') == 0) url = url.substr(7);
else if (url.indexOf('https://') == 0) url = url.substr(8);
if (url.indexOf('www.') == 0) url = url.substr(4);
return url;


return url.replace(/^(https?:\/\/)?(www\.)?/, '');


You could use this regular expression replacement:

var url = "http://piza.com/something/1";
var output = url.replace(/^http(s)?:\/\/(www\.)?/, "");

To explain the regular expression:

^http - string starts with "http"
(s)?  - followed by an optional "s" in "https"
:\/\/ - followed by "://"
(www\.) - followed an an optional "www."

Then, replace whatever part of that match that you found with nothing, leaving only the rest of the URL.


I will also add to the plethora of answers (since I worked it out).

A small modification to your original script will work:

^(http(s)?:\/\/)?(www\.)?(([^\/]+)(\/.*)?)$

so you'd have

function cleanUrl(url) { 
    return url.replace(/^(http(s)?:\/\/)?(www\.)?(([^\/]+)(\/.*)?)$/gi,"$4");
}

The main difference here is I changed the part just after the www check to capture the rest of your regular expression as a group. It's really all about groups.

Your original string / regex combo ^(http(s)?:\/\/)?(www\.)?([^\/]+)(\/.*)?$ returned the following groups:

  • 0. http://piza.com/something/1
  • 1. http://
  • 2. undefined
  • 3. undefined
  • 4. pizza.com
  • 5. /something/1

  • The revised string /regex combo ^(http(s)?:\/\/)?(www\.)?(([^\/]+)(\/.*)?)$ returns:

  • 0. http://piza.com/something/1
  • 1. http://
  • 2. undefined
  • 3. undefined
  • 4. pizza.com/something/1
  • 5. pizza.com
  • 6. /something/1

  • Any time you have a parenthesis pair you create a group. In your case, you wanted a group that captured everything after the www part, so that is where I made a new group to capture everything else.

    BTW, you can easily try out these regex's online. I used JavaScript RegExp Example: Regular Expression Tester and hit the "Show Match" button to see what your groups were returning.

    0

    上一篇:

    下一篇:

    精彩评论

    暂无评论...
    验证码 换一张
    取 消

    最新问答

    问答排行榜