javascript 纯正则按需拿到url里的参数,兼容hash
文章目录
直接上代码吧
var getParmasByKey = (url = location.href, key='') => (url.match(new RegExp(('([&?])'+key+'=([^&#]*)')))||[])[2]
var url = 'http://localhost/test?name=wind&id=50012&age=27&test=&dd=22&cc=#first'
console.log(getParmasByKey(url, 'name'))
console.log(getParmasByKey(url, 'id'))
console.log(getParmasByKey(url, 'age'))
console.log(getParmasByKey(url, 'test'))
console.log(getParmasByKey(url, 'dd'))
console.log(getParmasByKey(url, 'cc'))
评论
登录后参与技术讨论,分享你的经验与见解。
登录 / 注册暂无评论