Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/472.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 删除URL哈希字符串中的前几个字符_Javascript_Hash - Fatal编程技术网

Javascript 删除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(

我试图找出如何删除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(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',)