Javascript用一个正斜杠替换后退和斜线斜杠的组合 [英] Javascript replace a combination of back and forward slash with a single forward slash

查看:131
本文介绍了Javascript用一个正斜杠替换后退和斜线斜杠的组合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Javascript中,我有一些麻烦用字符串中的'/'替换'/'。我试过这个

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

这是什么问题?我正在逃避转义字符,即反斜杠。



请指教。感谢
编辑:我有一个转义的URL,如http://www.gogobeans.com。我需要将其更改为 http://www.gogobeans.com

解决方案

我从未听说过纯粹的javascript中的 replaceAll 方法,尝试替换正则表达式:

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

我已经使用 \\ 然后转义范围字符 / ,它应该工作。



JSFiddle: 演示版


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

解决方案

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

这篇关于Javascript用一个正斜杠替换后退和斜线斜杠的组合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆