How can I use backslashes (\) in a string?

前端 未结 3 1052
走了就别回头了
走了就别回头了 2020-11-22 05:20

I tried many ways to get a single backslash from an executed (I don\'t mean an input from html).

I can get

相关标签:
3条回答
  • 2020-11-22 05:38

    (See ES2015 update at the end of the answer.)

    You've tagged your question both string and regex.

    In JavaScript, the backslash has special meaning both in string literals and in regular expressions. If you want an actual backslash in the string or regex, you have to write two: \\.

    This string starts with one backslash, the first one you see in the literal is an escape character telling us to take the next character literally:

    var str = "\\I have one backslash";
    

    This regular expression will match a single backslash (not two); again, the first one you see in the literal is an escape character telling us to take the next character literally:

    var rex = /\\/;
    

    If you're using a string to create a regular expression (rather than using a regular expression literal as I did above), note that you're dealing with two levels: The string level, and the regular expression level. So to create a regular expression using a string that matches a single backslash, you end up using four:

    // Matches *one* backslash
    var rex = new RegExp("\\\\");
    

    That's because first, you're writing a string literal, but you want to actually put backslashes in it. So you do that with \\ for each one backslash you want. But your regex also requires two \\ for every one real backslash you want, and so it needs to see two backslashes in the string. Hence, a total of four. This is one of the reasons I avoid using new RegExp(string) whenver I can; I get confused easily. :-)

    ES2015 update

    Fast-forward to 2015, and as Dolphin_Wood points out the new ES2015 standard gives us template literals, tag functions, and the String.raw function:

    // Yes, this unlikely-looking syntax is actually valid ES2015
    let str = String.raw`\apple`;
    

    str ends up having the characters \, a, p, p, l, and e in it. Just be careful there are no ${ in your "string" (template), since this is a template literal and ${ starts a substitution. E.g.:

    let foo = "bar";
    let str = String.raw`\apple${foo}`;
    

    ...ends up being \applebar. Also note this isn't quite like "verbatim" strings in C# or similar, as sequences matching the LegacyOctalEscapeSequence in the spec are not allowed and cause a syntax error. So for instance

    // Fails
    let str = String.raw`c:\foo\12\bar`;
    

    ...fails because \12 looks like a legacy octal literal.

    0 讨论(0)
  • 2020-11-22 05:45

    Try String.raw method:

    str = String.raw`\apple` // "\apple"
    

    Reference here: String.raw()

    0 讨论(0)
  • 2020-11-22 05:57

    \ is an escape character, when followed by a non-special character it doesn't become a literal \. Instead, you have to double it \\.

    console.log("\apple");  //-> "apple" 
    console.log("\\apple"); //-> "\apple" 
    

    There is no way to get the original, raw string definition or create a literal string without escape characters.

    0 讨论(0)
提交回复
热议问题