开发者

javascript strip words from url

i got a little book marklet

javascript:window.location='http://alexa.com/siteinfo/'+window.location.host;

when clicked it takes you to alexa.com/siteinfo/www.thesiteyouwereon.com

i dont know a lot about js regex..is there a way to get rid of the www. from the begining of the sit开发者_Python百科e. so i can get alexa.com/siteinfo/nowwwsite.com

thanks for the help..sorry if this was stupid q.


Just remove www: String(window.location.host).replace(/^www\./,'')

Remove http, https, and www: String(window.location.host).replace(/^(https?:\/\/)?(www\.)?/,'')

Regex is really worth learning. Really.

Explanation of regex:

  • ^ means at the start of the input (either line, or string depending on the m modifier). And $ means at the end.
  • ? means optional, or if used with * or + means least
  • * means zero or more
  • + means one or more
  • . means any character, so if you want jut a dot, then use \. as it must be escaped.
  • [a-z0-9_] means any a to z, 0 to 9, and a underscore character.
  • [^a-z] means anything that is not a to z character.
  • () surrounds a group
  • $1 references the first group
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜