【发布时间】:2018-02-10 20:29:39
【问题描述】:
我正在尝试使用正则表达式删除 url 中的特定参数。
//Here are the scenarios of what I want to remove in the url
'?pIds=123,2311' => ''
'?pIds=123,2311&deal=true' => '?deals=true'
'?pIds=123' => ''
'?pIds=123&deals=true' => '?deals=true'
'&pIds=123,2311' => ''
'&pIds=123,2311&deals=true' => '&deals=true'
'&pIds=123' => ''
'&pIds=123&deals=true' => '&deals=true'
const a = '?pIds=123,2311&deals=true';
a.replace(/&?pIds=\d+,?\d+/i, '');
是否可以为这些场景创建单个正则表达式?我怎样才能有条件地拥有?或者 & 如果 pIds 分别是第一个或中间参数?
【问题讨论】:
-
我建议将查询字符串解析成一个不错的地图/字典结构,删除不需要的键,然后重新创建查询字符串。
标签: javascript regex url