简体   繁体   中英

How do i parse this string in javascript

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

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"

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

Format

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

You can just replace the other part:

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

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

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

Displays "200bfaa1f5d6cda4c782f98b15f32e7f"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM