【发布时间】:2017-11-14 11:59:32
【问题描述】:
我希望通过点击事件从 URL 中删除参数。问题是参数前面可以有&,也可以没有。所以表单是search=MYSEARCHTERM 或&search=MYSEARCHTERM。
我有以下似乎适用于一个或另一个但不能同时适用的以下内容。我在想我可以有一个 if / else 语句,其中一个包含这样的东西。 (请原谅蹩脚的正则表达式,但我以前从未写过)
var searchKeywordRegx = new RegExp(/(?:&)/ + 'search=' + searchKeyword);
$('.searchKeyword').click(function() {
$(this).remove();
var searchKeywordRegx = new RegExp('search=' + searchKeyword);
console.log(searchKeywordRegx);
document.location.href = String( document.location.href ).replace(searchKeywordRegx , "" );
});
我离基地还远吗?
【问题讨论】:
标签: javascript jquery regex url