Javascript replace a combination of back and forward slash with a single forward slash

瘦欲@ 提交于 2019-12-25 05:28:52

问题


in Javascript, I am having some trouble replacing '/' with '/' in a string. I tried this

string.replaceAll("\\/","/"));

What's wrong with this ? I am escaping the escape character i.e. the backslash.

Please advise. Thanks Edit: I have an escaped URL such as http://www.gogobeans.com. I need to change this to http://www.gogobeans.com


回答1:


I've never heard of replaceAll method in pure javascript, try replace with a regex:

 string.replace(/\\\//g, "/");

I've escaped the escape character with \\ then escaped the scope character /, it should work.

JSFiddle: DEMO



来源:https://stackoverflow.com/questions/10507770/javascript-replace-a-combination-of-back-and-forward-slash-with-a-single-forward

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!