直接上代码吧

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'))

杨龙飞
45 声望2 粉丝

喜欢思考,喜欢前端,喜欢交友,喜欢玩