Javascript - 替换字符串中的转义字符
我正在尝试替换 Javascript 字符串文字中的反斜杠(转义)字符。
我需要用双反斜杠替换它,以便我可以进行重定向:
var newpath = 'file:///C:\funstuff\buildtools\viewer.html'.replace(/\\/g,"\\");
window.location = newpath;
但是,它似乎没有结果。
在 Javascript 处理反斜杠之前,我无法正确转义它们。
如何用 (\\) 替换 (\) 以便 Javascript 满意?
谢谢, 德里克
I am trying to replace the backslash (escape) character in a Javascript string literal.
I need to replace it with a double backslash so that I can then do a redirect:
var newpath = 'file:///C:\funstuff\buildtools\viewer.html'.replace(/\\/g,"\\");
window.location = newpath;
However, it seems to have no result.
I don't have the option of properly escaping the backslashes before they are handled by Javascript.
How can I replace (\) with (\\) so that Javascript will be happy?
Thanks,
Derek
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果它是一个文字,则需要在 Javascript 看到反斜杠之前转义它们;没有办法解决这个问题。
如果 newpath 从其他地方获取其值,并且确实包含单个反斜杠,则不需要将它们加倍;但如果出于某种原因你确实想要这样做,请不要忘记在replace()调用中转义反斜杠:
为什么在Javascript处理反斜杠之前你没有选择正确转义反斜杠?如果问题是您的 Javascript 源代码是从其他脚本语言生成的,而该语言本身使用 \ 作为转义字符,则只需添加转义级别:
If it's a literal, you need to escape the backslashes before Javascript sees them; there's no way around that.
If
newpath
is getting its value from somewhere else, and really does contain single backslashes, you don't need to double them up; but if you really wanted to for some reason, don't forget to escape the backslashes in the replace() call:Why do you not have the option of properly escaping the backslashes before they are handled by Javascript? If the problem is that your Javascript source is being generated from some other scripting language that itself uses \ as an escape character, just add a level of escaping:
您应该替换为“\\\\”,因为“\\”转义为单个 \,因此没有变化。
You should be replacing with "\\\\" because "\\" is escaping into a single \ thus no change.
为了更好地演示和理解字符串转义行为,请看以下示例:
通过分割字符串,您可以看到 JS 引擎解析后的字符串在内存中的样子,从而也提供了潜在的可能性围绕这个问题的(丑陋的)解决方案:
基本上,如果不注意转义上下文,单个反斜杠会转义以下字符,从而导致意外结果。
解决方案:
通过查找表,您可以恢复许多错误转义的字符(如果这些字符位于
\x20-\x7F
的可打印 ASCII 字符范围之外)。例如,对于上面的示例,12
或\x0c
[12..toString(16)
] 将变为'\\' +'v'
,等等。PS:请注意,发生了信息丢失,并且您正在尝试通过上下文或元信息恢复信息,这意味着在您的情况下,字符串位于可打印的 ASCII 范围。
请与社区分享任何实现。干杯!
To better demonstrate and understand the string-escaping behavior, take the following example:
You can see what the string looks like in memory after being parsed by the JS-engine by splitting the string, thus also offering potential (ugly) solutions around this issue:
Basically the single backslash escapes the following character, thus giving rise to unexpected results, if the escaping-context is not heeded.
Solution:
Through a look-up-table, you can restore many errantly escaped characters if they lie outside the printable ASCII character range of
\x20-\x7F
. For the example above for instance,12
or\x0c
[12..toString(16)
] would become'\\'+'v'
, and so on.PS: Be aware that a loss of information occured, and you are trying to restore information through contextual- or meta- information, meaning in your case that the string is in the printable ASCII range.
Please share any implementations with the community. Cheers!
<<转义字符替换>>
<< Escape Characters Replacement>>