"?route=system/template/update&template_id=22&token=200bfaa1f5d6cda4c782f98b15f32e7f"
,我只需要標記出它
200bfaa1f5d6cda4c782f98b15f32e7f
如何是最好的方式解析了...看來永遠是最後
"?route=system/template/update&template_id=22&token=200bfaa1f5d6cda4c782f98b15f32e7f"
,我只需要標記出它
200bfaa1f5d6cda4c782f98b15f32e7f
如何是最好的方式解析了...看來永遠是最後
這應做到:
var
input = "?route=system/template/update&template_id=22&token=200bfaa1f5d6cda4c782f98b15f32e7f",
output = input.split("=").splice(-1)[0]; // output === "200bfaa1f5d6cda4c782f98b15f32e7f"
或者,如果你不知道該令牌總是最後一個值:
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"
作品很好尤其是因爲令牌總是最後。其他方面,這是行不通的。 – g19fanatic 2011-04-25 14:08:07
或者使用正則表達式。在這種情況下,它會像
格式
var re = /token\=(\S+)/i;
alert(url.match(re)[1]);
或... alert(input.match(/ token =(\ w +)/)[1]) – 2011-04-25 14:10:51
您只需更換另一部分:
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]*$");
顯示"200bfaa1f5d6cda4c782f98b15f32e7f"
正如你可以看到許多問題的答案改變他們的方法,有些可能不適合,如果URL甚至略有不同,這主要是因爲答案是指t o這個特定的情況,並沒有其他的變種,因爲他們不知道他們,如果這是唯一可能的「形式」,你可能會遇到的URL,這很好,但如果你最後可能'&otherthing = 123',有些可能無效,也許澄清會有所幫助。 – Trufa 2011-04-25 14:17:49
你的權利,我注意到有可能在最後得到var ...所以我去這個input.match(/令牌=(\ w +)/)[0123] – Trace 2011-04-25 14:21:00