本文介绍了Javascript哈希替换错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这有什么问题?



散列是:#/ search = hello / somethingelse /

 >  hash.replace()实际上并没有改变哈希,只返回一个值因为它是一个字符串函数)。尝试分配该结果,使用: 

  window.location.hash = window.location.hash.replace(/ search =([ ^ \ /] *)/ gi,search =+ value); 

另一方面, window.location.replace() 实际上是一个改变URL的函数,但这不能直接与正则表达式一起工作。


What's the problem with this?

the hash is : #/search=hello/somethingelse/

window.location.hash.replace(/search=([^\/]*)/gi, "search=" + value);

EDIT:

I want to change just a specific part of the hash not the whole hash.

解决方案

hash.replace() does not actually change the hash, only return a value (as it is a String function). Try assigning that result, using:

window.location.hash = window.location.hash.replace(/search=([^\/]*)/gi, "search=" + value);

On the other hand, window.location.replace() is actually a function that changes the URL, but that does not work directly with regexes.

这篇关于Javascript哈希替换错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-25 03:53