简体   繁体   English

Javascript - 替换字符串文字中的转义字符

[英]Javascript - Replacing the escape character in a string literal

I am trying to replace the backslash (escape) character in a Javascript string literal.我正在尝试替换 Javascript 字符串文字中的反斜杠(转义)字符。

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.在 Javascript 处理反斜杠之前,我没有正确转义反斜杠的选项。

How can I replace (\) with (\\) so that Javascript will be happy?我怎样才能用 (\\) 替换 (\) 以便 Javascript 会很高兴?

Thanks, Derek谢谢,德里克

If it's a literal, you need to escape the backslashes before Javascript sees them;如果它是文字,你需要在 Javascript 看到它们之前转义反斜杠; there's no way around that.没有办法解决这个问题。

var newpath = 'file:///C:\\funstuff\\buildtools\\viewer.html';
window.location = newpath;

If newpath is getting its value from somewhere else, and really does contain single backslashes, you don't need to double them up;如果newpath从其他地方获取它的值,并且确实包含单个反斜杠,则不需要将它们加倍; but if you really wanted to for some reason, don't forget to escape the backslashes in the replace() call:但是如果出于某种原因你真的想要这样做,请不要忘记在 replace() 调用中转义反斜杠:

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

Why do you not have the option of properly escaping the backslashes before they are handled by Javascript?为什么您不能选择在 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:如果问题是您的 Javascript 源代码是从其他一些本身使用 \ 作为转义字符的脚本语言生成的,只需添加一个转义级别:

var newpath = 'file:///C:\\\\funstuff\\\\buildtools\\\\viewer.html';

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:通过拆分字符串,您可以看到字符串在被 JS 引擎解析后在内存中的样子,因此也提供了解决此问题的潜在(丑陋)解决方案:

'file:///C:\funstuff\buildtools\viewer.html'.split('')
//>
 ["f", "i", "l", "e", ":", "/", "/", "/", "C", ":", "", "u", "n", "s", "t", "u",
  "f", "f", "", "u", "i", "l", "d", "t", "o", "o", "l", "s", "", "i", "e", "w",
  "e", "r", ".", "h", "t", "m", "l"]

'file:///C:\funstuff\buildtools\viewer.html'.split('').map( function(e){
     return e.charCodeAt()
});
//>
[102, 105, 108, 101, 58, 47, 47, 47, 67, 58, 12, 117, 110, 115, 116, 117, 102,
 102, 8, 117, 105, 108, 100, 116, 111, 111, 108, 115, 11, 105, 101, 119, 101, 
 114, 46, 104, 116, 109, 108]
//>in Hex values by applying .toString(16)
["66", "69", "6c", "65", "3a", "2f", "2f", "2f", "43", "3a", "c", "75", "6e", 
 "73", "74", "75", "66", "66", "8", "75", "69", "6c", "64", "74", "6f", "6f", 
 "6c", "73", "b", "69", "65", "77", "65", "72", "2e", "68", "74", "6d", "6c"]

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 .通过查找表,您可以恢复许多错误转义的字符,如果它们位于\x20-\x7F的可打印 ASCII 字符范围之外。 For the example above for instance, 12 or \x0c [ 12..toString(16) ] would become '\\'+'v' , and so on.例如,对于上面的示例, 12\x0c [ 12..toString(16) ] 将变为'\\'+'v' ,依此类推。

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. PS:请注意发生了信息丢失,并且您正在尝试通过上下文或元信息恢复信息,这意味着在您的情况下该字符串在可打印的 ASCII 范围内。

Please share any implementations with the community.请与社区分享任何实施。 Cheers!干杯!

You should be replacing with "\\\\" because "\\" is escaping into a single \ thus no change.您应该替换为“\\\\”,因为“\\”正在转义为单个 \,因此没有变化。

<< Escape Characters Replacement>> << 转义字符替换>>

import java.util.Scanner;

public class Example7 {

  public static void main(String[] args) {
    Scanner in=new Scanner(System.in);
    System.out.println("Please enter a sentence: ");
    String a=in.nextLine();
    //System.out.println("the word had enter: "+a);

    String Str1 = a.replace("\\n", "(new_line)");
    //System.out.println(Str1);
    String Str2 = Str1.replace("\\t", "(tab)");
    //System.out.println(Str2);
    String Str3 = Str2.replace("\\t", "(tab)");
    String Str4 = Str3.replace("\\\\", "(comment_line)");
    String Str5 = Str4.replace(":)", "(smile) ");
    System.out.println("The new sentence:"  +Str5);
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM