使用javascript将反斜杠转换为正斜杠无法正常工作? [英] Converting backslashes into forward slashes using javascript does not work properly?

查看:189
本文介绍了使用javascript将反斜杠转换为正斜杠无法正常工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个javascript变量从遗留系统传入反斜杠到正斜杠:

I have a javascript variable comming from legacy system with backslashes into forward slashes:

'/ 46 \465531_Thumbnail.jpg'

我想兑换成这个:

'/46/465531_Thumbnail.jpg'

无法解决遗留系统上的问题。

There is no way to fix the problem on the legacy system.

这是我在IE8浏览器上运行的命令:

Here is the command I am running on IE8 browser:

javascript:alert("/46\465531_Thumbnail.jpg".replace(/\\/g,"/"));

作为我得到的回复:

---------------------------
Message from webpage
---------------------------
/46&5531_Thumbnail.jpg
---------------------------
OK   
---------------------------

实际上我只想翻译为'/ 46 / 465531_Thumbnail.jpg '

有什么问题?

推荐答案

你需要在字符串常量中加倍反斜杠:

You need to double the backslash in your string constant:

alert("/46\\465531_Thumbnail.jpg".replace(/\\/g,"/"));

如果您的遗留系统实际上是在您的网页上创建了包含嵌入式,未引用的JavaScript字符串常量(是,而不是加倍)这样的反斜杠,然后它被打破,你会遇到问题。但是,如果您通过XML或JSON或其他类型的某种ajax调用获取字符串,那么您的代码看起来没问题。

If your legacy system is actually creating JavaScript string constants on your pages with embedded, un-quoted (that is, not doubled) backslashes like that, then it's broken and you'll have problems. However, if you're getting the strings via some sort of ajax call in XML or JSON or whatever, then your code looks OK.

这篇关于使用javascript将反斜杠转换为正斜杠无法正常工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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