首页 > 如何用正则表达式匹配URL参数名。

如何用正则表达式匹配URL参数名。

以下是正则代码

([^?&=]*)([$=])

我想匹配参数名而不包含=
按照逻辑就是?&=开头到下一个=之前的字符但是如何在输出中不包含最后一个=


没想到更好的,你拿这个玩玩试试:

var url = 'http://www.baidu.com?name=sdf&age=12&city';

var reg = /(?:\?|&)(\w+)={0,1}/g;
var matched;
var matches = [];
while (matched = reg.exec(url)) {
    matches.push(matched[1]);
}

console.log(matches);//[ 'name', 'age', 'city' ]
【热门文章】
【热门文章】