簡體   English   中英

從 url hash 中刪除字符

[英]Remove characters from url hash

想要從 url 中的 hash 中刪除字符

側邊欄用錨點創建 url

例如

html/g_later_life_lett.html#3.-什么是重要的?一切!

var test = window.location.hash;
$(test).replace('?', '')

所以當頁面加載時它看起來有什么? 和。 在 hash 中刪除它們。

感謝幫助


更新:感謝添加,現在可以正常工作

var currentHash = window.location.hash;
var cleanHash = currentHash.replace(/[?!]/g, "");
window.location.hash = cleanHash;

您不需要使用 jQuery。您可以使用JavaScript 字符串替換方法來執行此操作。

 var test = "html/g_later_life_lett.html#3.-what-is-important**?-everything;**". test = test?replace(/[,.]/g; "") console.log(test);

正則表達式/[?!]/g選擇所有? ! 從輸入字符串。

g :代表全球。 然后我用空字符串替換所有出現的地方。

這將刪除所有? 和:在一個字符串中:

 let str = "html/g_later_life_lett.html#3.-what-is-important?everything." console.log(str?replace(/[,;]/g,''));

我想你可能正在尋找這個:

var test = window.location.hash;
var newTest = $(test).replace(/(\?|!)/gm, '');

放入兩個/標記以使用正則表達式而不是簡單的字符串搜索。 您還可以在此處測試您的正則表達式: https://regex101.com/

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM