我不确定这是否可能说我有这样的事情......
/account/register#id_token=eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsImtpZCI6Ik56RXhNemRETUVRME9EWkdOMFJHUmtRMk9VUXdNemxFUWpWQk1UaEdNVFpCTURneU0wWXhSZyJ9.eyJuaWNrbmFtZSI6ImhlbGxvIiwibmFtZSI6ImhlbGxvQGhlbGxvLmNvbSIsInBpY3R1cmUiOiJodHRwczovL3MuZ3JhdmF0YXIuY29tL2F2YXRhci9kMzZlZWYzMjYyNTM2N2E0M2RhNjNhOTkwOWJhNzE2ZT9zPTQ4MCZyPXBnJmQ9aHR0cHMlM0ElMkYlMkZjZG4uYXV0aDAuY29tJTJGYXZhdGFycyUyRmhlLnBuZyIsInVwZGF0ZWRfYXQiOiIaGlyc3R5c3R1ZGlvcy5hdS5hdXRoMC5jb20vIiwic3ViIjoiYXV0aDB8NWFkNmE4NjI5MDI1MDU0NzJjNTYzMzBiIiwiYXVkIjoia3A1Zk1kaXNCdjRFcnc1dFJoN29LWlZSOTAxQ3BRZGUiLCJpYXQiOjE1MjQwMTcyNTUsImV4cCI6MTUyNDA1MzI1NSwibm9uY2UiOiIxSngxNmRHcjdOUjhla2xIZkNheWwyTDlYZWVSMkJNcCJ9.UBileHlEfQbuQgOPk60EkpTHAxxC_o0-4C4No6sCLbqTwEHljLKcKhA1CQhcMdtcn2a1c1YnV1Ka5AdYxh65iKGOEqMcauhIFKXkeO2E_X_mpv9x26HJzEJVzsg3PKzGKE8iAthzp_wXZf8Xqgnt7XAeKh6rTvpigZ2yjMdykCHu-rZgokaPO1CuYsFSibPf_ewS3zMHVbPGd1M2k-wvWiIV9anudkNLG4H0luEyBkrzNtKaa5x0z-YdNKhlApetcMC2QRbuHFbBGrCaQNTUJ5lnu-pMeT6llnRpLT9oE08elVcuEppXCXFRl-JFkbcWp-ft5BSOeTNkavEwb_jpJg&state=tBo7J0ySFXKMuVkKrdj3FURzoG5Y7Hu6
有没有办法可以在=
之后将字符串拆分为#id_token
,只将令牌保存到变量中,然后只丢弃其余的?
我知道我可以在=
拆分并将两半放在一个数组中然后token = array[1]
我只是想知道是否有更好的方法
答案 0 :(得分:4)
使用内置的URLSearchParams
而不是自己搞乱字符串解析:
const input = '/account/register#id_token=eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsImtpZCI6Ik56RXhNemRETUVRME9EWkdOMFJHUmtRMk9VUXdNemxFUWpWQk1UaEdNVFpCTURneU0wWXhSZyJ9.eyJuaWNrbmFtZSI6ImhlbGxvIiwibmFtZSI6ImhlbGxvQGhlbGxvLmNvbSIsInBpY3R1cmUiOiJodHRwczovL3MuZ3JhdmF0YXIuY29tL2F2YXRhci9kMzZlZWYzMjYyNTM2N2E0M2RhNjNhOTkwOWJhNzE2ZT9zPTQ4MCZyPXBnJmQ9aHR0cHMlM0ElMkYlMkZjZG4uYXV0aDAuY29tJTJGYXZhdGFycyUyRmhlLnBuZyIsInVwZGF0ZWRfYXQiOiIaGlyc3R5c3R1ZGlvcy5hdS5hdXRoMC5jb20vIiwic3ViIjoiYXV0aDB8NWFkNmE4NjI5MDI1MDU0NzJjNTYzMzBiIiwiYXVkIjoia3A1Zk1kaXNCdjRFcnc1dFJoN29LWlZSOTAxQ3BRZGUiLCJpYXQiOjE1MjQwMTcyNTUsImV4cCI6MTUyNDA1MzI1NSwibm9uY2UiOiIxSngxNmRHcjdOUjhla2xIZkNheWwyTDlYZWVSMkJNcCJ9.UBileHlEfQbuQgOPk60EkpTHAxxC_o0-4C4No6sCLbqTwEHljLKcKhA1CQhcMdtcn2a1c1YnV1Ka5AdYxh65iKGOEqMcauhIFKXkeO2E_X_mpv9x26HJzEJVzsg3PKzGKE8iAthzp_wXZf8Xqgnt7XAeKh6rTvpigZ2yjMdykCHu-rZgokaPO1CuYsFSibPf_ewS3zMHVbPGd1M2k-wvWiIV9anudkNLG4H0luEyBkrzNtKaa5x0z-YdNKhlApetcMC2QRbuHFbBGrCaQNTUJ5lnu-pMeT6llnRpLT9oE08elVcuEppXCXFRl-JFkbcWp-ft5BSOeTNkavEwb_jpJg&state=tBo7J0ySFXKMuVkKrdj3FURzoG5Y7Hu6';
const params = new URLSearchParams(input);
console.log(params.get('/account/register#id_token'));

如果您需要支持IE,请同时使用polyfill。
答案 1 :(得分:1)
我不确定是否有更好的方式,因为这是主观的。如果你想比使用=
更具体,你可以使用更明确的正则表达式模式:
let string = '#id_token=eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsImtpZCI6Ik56RXhNemRETUVRME9EWkdOMFJHUmtRMk9VUXdNemxFUWpWQk1UaEdNVFpCTURneU0wWXhSZyJ9.eyJuaWNrbmFtZSI6ImhlbGxvIiwibmFtZSI6ImhlbGxvQGhlbGxvLmNvbSIsInBpY3R1cmUiOiJodHRwczovL3MuZ3JhdmF0YXIuY29tL2F2YXRhci9kMzZlZWYzMjYyNTM2N2E0M2RhNjNhOTkwOWJhNzE2ZT9zPTQ4MCZyPXBnJmQ9aHR0cHMlM0ElMkYlMkZjZG4uYXV0aDAuY29tJTJGYXZhdGFycyUyRmhlLnBuZyIsInVwZGF0ZWRfYXQiOiIaGlyc3R5c3R1ZGlvcy5hdS5hdXRoMC5jb20vIiwic3ViIjoiYXV0aDB8NWFkNmE4NjI5MDI1MDU0NzJjNTYzMzBiIiwiYXVkIjoia3A1Zk1kaXNCdjRFcnc1dFJoN29LWlZSOTAxQ3BRZGUiLCJpYXQiOjE1MjQwMTcyNTUsImV4cCI6MTUyNDA1MzI1NSwibm9uY2UiOiIxSngxNmRHcjdOUjhla2xIZkNheWwyTDlYZWVSMkJNcCJ9.UBileHlEfQbuQgOPk60EkpTHAxxC_o0-4C4No6sCLbqTwEHljLKcKhA1CQhcMdtcn2a1c1YnV1Ka5AdYxh65iKGOEqMcauhIFKXkeO2E_X_mpv9x26HJzEJVzsg3PKzGKE8iAthzp_wXZf8Xqgnt7XAeKh6rTvpigZ2yjMdykCHu-rZgokaPO1CuYsFSibPf_ewS3zMHVbPGd1M2k-wvWiIV9anudkNLG4H0luEyBkrzNtKaa5x0z-YdNKhlApetcMC2QRbuHFbBGrCaQNTUJ5lnu-pMeT6llnRpLT9oE08elVcuEppXCXFRl-JFkbcWp-ft5BSOeTNkavEwb_jpJg&state=tBo7J0ySFXKMuVkKrdj3FURzoG5Y7Hu6'
let token = string.match(/#id_token=(.*)/).pop()
console.log(token)

答案 2 :(得分:0)
使用String方法 substring()
const string = '#id_token=eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsImtpZCI6Ik56RXhNemRETUVRME9EWkdOMFJHUmtRMk9VUXdNemxFUWpWQk1UaEdNVFpCTURneU0wWXhSZyJ9.eyJuaWNrbmFtZSI6ImhlbGxvIiwibmFtZSI6ImhlbGxvQGhlbGxvLmNvbSIsInBpY3R1cmUiOiJodHRwczovL3MuZ3JhdmF0YXIuY29tL2F2YXRhci9kMzZlZWYzMjYyNTM2N2E0M2RhNjNhOTkwOWJhNzE2ZT9zPTQ4MCZyPXBnJmQ9aHR0cHMlM0ElMkYlMkZjZG4uYXV0aDAuY29tJTJGYXZhdGFycyUyRmhlLnBuZyIsInVwZGF0ZWRfYXQiOiIaGlyc3R5c3R1ZGlvcy5hdS5hdXRoMC5jb20vIiwic3ViIjoiYXV0aDB8NWFkNmE4NjI5MDI1MDU0NzJjNTYzMzBiIiwiYXVkIjoia3A1Zk1kaXNCdjRFcnc1dFJoN29LWlZSOTAxQ3BRZGUiLCJpYXQiOjE1MjQwMTcyNTUsImV4cCI6MTUyNDA1MzI1NSwibm9uY2UiOiIxSngxNmRHcjdOUjhla2xIZkNheWwyTDlYZWVSMkJNcCJ9.UBileHlEfQbuQgOPk60EkpTHAxxC_o0-4C4No6sCLbqTwEHljLKcKhA1CQhcMdtcn2a1c1YnV1Ka5AdYxh65iKGOEqMcauhIFKXkeO2E_X_mpv9x26HJzEJVzsg3PKzGKE8iAthzp_wXZf8Xqgnt7XAeKh6rTvpigZ2yjMdykCHu-rZgokaPO1CuYsFSibPf_ewS3zMHVbPGd1M2k-wvWiIV9anudkNLG4H0luEyBkrzNtKaa5x0z-YdNKhlApetcMC2QRbuHFbBGrCaQNTUJ5lnu-pMeT6llnRpLT9oE08elVcuEppXCXFRl-JFkbcWp-ft5BSOeTNkavEwb_jpJg&state=tBo7J0ySFXKMuVkKrdj3FURzoG5Y7Hu6';
const token = x.substring('#id_token='.length);
substring()的文档: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/substring