andrewk
andrewk

Reputation: 3871

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 site. so i can get alexa.com/siteinfo/nowwwsite.com

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

Upvotes: 2

Views: 4361

Answers (1)

balupton
balupton

Reputation: 48650

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

Upvotes: 8

Related Questions