0

this url

"?route=system/template/update&template_id=22&token=200bfaa1f5d6cda4c782f98b15f32e7f"

and i just need the token out of it

 200bfaa1f5d6cda4c782f98b15f32e7f

how is the best way to parse that out...it seems to always be last

2
  • As you can see many of the answers vary their approach and some may not fit if the URL is even slightly different, this is mainly because the answers are referring to this specific case and no other variants since they don't know about them, if this is the only possible "form" you could encounter the URL, this is fine, but if you may have &otherthing=123 at the end, some may not work, maybe a clarification would help. Commented Apr 25, 2011 at 14:17
  • your right i noticed that there maybe another get var at the end...so i went with this input.match(/token=(\w+)/)[1] Commented Apr 25, 2011 at 14:21

4 Answers 4

2

This should do it:

var
  input = "?route=system/template/update&template_id=22&token=200bfaa1f5d6cda4c782f98b15f32e7f",
  output = input.split("=").splice(-1)[0]; // output === "200bfaa1f5d6cda4c782f98b15f32e7f"

Or, if you're not sure that the token is always the last value:

var 
  input = "?route=system/template/update&template_id=22&token=200bfaa1f5d6cda4c782f98b15f32e7f&foo=bar&baz=spam",
  output = input.substring(input.indexOf('token=')).split(/[=&]/)[1]; // output === "200bfaa1f5d6cda4c782f98b15f32e7f"
Sign up to request clarification or add additional context in comments.

1 Comment

works great esp since the token is always last. Other wise this will not work.
1

Or use regular expression. In this case it will be like

Format

var re = /token\=(\S+)/i;
alert(url.match(re)[1]);

1 Comment

or... alert(input.match(/token=(\w+)/)[1])
0

You can just replace the other part:

s = "?route=system/template/update&template_id=22&token=200bfaa1f5d6cda4c782f98b15f32e7f"

s.replace(/.*token=/, '')

#=> "200bfaa1f5d6cda4c782f98b15f32e7f"

Comments

0
s = "?route=system/template/update&template_id=22&token=200bfaa1f5d6cda4c782f98b15f32e7f";
s.match("[0-9a-z]*$");

Displays "200bfaa1f5d6cda4c782f98b15f32e7f"

Comments

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.