我正在尋找通過點擊事件從網址中移除參數。問題是該參數可能在其之前或之前有一個&
。所以表格是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 , "");
});
我是大錯特錯嗎?
啊!很簡單。謝謝 – LMG