Javascript 删除URL哈希字符串中的前几个字符
我试图找出如何删除URL字符串的某个部分,如:Javascript 删除URL哈希字符串中的前几个字符,javascript,hash,Javascript,Hash,我试图找出如何删除URL字符串的某个部分,如: if (window.location.hash == '#super-super-product') { change.window.location.hash.to.this: #product // pseudo code, obviously } 因此,要删除“super-super-”,即前12个字符,并保留其余字符, 不管它是什么 以下尝试不会产生任何变化: if (/^#checkout-counter-./.test(
if (window.location.hash == '#super-super-product') {
change.window.location.hash.to.this: #product // pseudo code, obviously
}
因此,要删除“super-super-”,即前12个字符,并保留其余字符,
不管它是什么
以下尝试不会产生任何变化:
if (/^#checkout-counter-./.test(window.location.hash)){ // this works perfectly
window.location.hash.substring(0, 11); // this does nothing
window.location.hash.substr(1, 12); // nothing
window.location.hash.slice(0, 11); // still nothing
}
谢谢。您需要重新分配它。否则,结果将被简单地丢弃,因为它不会被分配到任何有意义的地方
window.location.hash = window.location.hash.substring(0, 11);
调用substring或任何其他类似方法只会计算函数并返回它,而不会产生任何影响。您需要将结果分配给窗口的哈希
window.location.hash=window.location.hash.substring(0,11)代码>为什么先检查正则表达式,然后使用字符串函数?您可以使用str.replace(regex',)