Take the 2-minute tour ×

In my url, i'm suppose to get the token number, but the token number may be Numeric or Alpha. I need to get the token always in different scenario. How do I achieve that using Regular Expressions?

Sample URLs:





How can I always get the token from these kind of URLs?

I tried this:


I am looking for :

?&token=1905477219/someother/stuff - in this case "1905477219"


&token=xyzbacsfhsaof - in this case "xyzbacsfhsaof" .. like so

But it's not working. Can any one can help me?

Thanks all, this is working fine for me:

var reg = window.location.href.match(/token=([^\/]*)/)[1];
share|improve this question

2 Answers 2

up vote 4 down vote accepted

You can use this pattern to match any token with a Latin letter or decimal digit:


Or this which will allow the token to contain any character other than /:


Note that unless you expect to capture multiple tokens, the global modifier (g) is not necessary.

share|improve this answer
I don't want the word "token=" - only the token number i am looking for.. –  3gwebtrain Jul 17 '13 at 6:25
@3gwebtrain You just have to change where you put the parenthesis; see my updated answer. –  p.s.w.g Jul 17 '13 at 6:26
I tested, still i am getting like this: "token=1905477219" - instead "1905477219" –  3gwebtrain Jul 17 '13 at 6:32
@3gwebtrain How are you getting that value? /token=([a-z0-9]*)/.exec("token=1905477219")[1] will result in "1905477219". –  p.s.w.g Jul 17 '13 at 6:34
it works fine to me: window.location.href.match(/token=([^\/]*)/)[1]; –  3gwebtrain Jul 17 '13 at 7:06

without the token


// all will return 1905477219

this will capture letters, numbers and underscores while stopping at the forward slash if present

share|improve this answer
ok, very thanks to you. –  3gwebtrain Jul 17 '13 at 6:48

Your Answer


