2

I am in need of two regular expressions. First of all I want to check if my URL contains the hashtag #videos. Then if it does, I want to get the value of the second #tag. That value could contain all kinds of characters;

http://local/default.php#videos#12345
http://local/default.php#videos#g5458f
http://local/default.php#videos#0-e4a5d

This is what I've got so far:

if (window.location.hash = 'videos') {
      var url = window.location.hash,
          id = url.match(regex-goes-here);  // output e.g string '12345'
}

(Not sure if my extremely simple check (window.location.hash = 'videos') will work on two hashtags..? There is probably a better way of checking the URL, if so, please do tell :-)

3
  • Okay obviously window.location.hash won't work if you have multiple hastags... Commented Jun 14, 2011 at 14:47
  • the second # is simply part of the hash, it's valid, eg. in case of 'local/default.php#videos#12345', window.location.hash will be #videos#12345 Commented Jun 14, 2011 at 14:49
  • Ahh! That's why window.location.hash === '#videos' returned false. Makes sense, thanks! Commented Jun 14, 2011 at 14:56

1 Answer 1

3

You can get an array of tags like this:

var tags = window.location.hash.replace(/^#/, '').split('#');

In case of http://local/default.php#videos#12345, tags[0] will be videos and tags[1] will be 12345. You can use tags.length to determine how many tags there are.

Sign up to request clarification or add additional context in comments.

1 Comment

Neat and simple - love it! Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.