在javascript中删除字符串中的反斜杠 [英] Removing backslashes from strings in javascript

查看:109
本文介绍了在javascript中删除字符串中的反斜杠的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这种格式的网址:



http:\ / \ / example.example.ru\ / u82651140\\ \\ / audio \ / song.mp3



如何从字符串中删除多余的\?我曾尝试string.replace(\,),但似乎没有做任何事情。如果你可以给我一个JavaScript正则表达式来解决这个问题,那也可以。我只需要能够捕获这个字符串时,它是在另一个字符串内。

解决方案

尝试:

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

这将特别匹配\ /模式,以便您不会无意中删除任何其他在URL中可能存在反斜杠(例如在散列部分)。

I have a url in this format:

http:\/\/example.example.ru\/u82651140\/audio\/song.mp3

How can I remove the extra "\"s from the string? I have tried string.replace("\","") but that does not seem to do anything. If you could give me a JavaScript regular expression that will catch this, that would also work too. I just need to be able capture this string when it is inside another string.

解决方案

Try:

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

This will specifically match the "\/" pattern so that you don't unintentionally remove any other backslashes that there may be in the URL (e.g. in the hash part).

这篇关于在javascript中删除字符串中的反斜杠的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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