This is because Javascript strings are UTF-16, which actually sees these as two characters ("".length === 2). If you are lucky, nothing is done about the first backslash. Escaping Double and Single Quotes. JavaScript uses the \(backslash) as an escape characters for: \' single quote \" double quote \ backslash \n new line \r carriage return \t tab \b backspace \f form feed Having recently written about character references in HTML and escape sequences in CSS, I figured it would be interesting to look into JavaScript character escapes as well. The string will be chopped to "We are the so-called ". JSON is based on a subset of the JavaScript Programming Language, therefore, JSON also uses backslash as the escape character: A string is a sequence of zero or more Unicode characters, wrapped in double quotes, using backslash escapes. It does not show backslash in the console but it does in the return value. for ? Switch to a general JavaScript context. The syntax of \' will always be a single quote, and the syntax of \" will always be a double quote, without any fear of breaking the string. The String#replaceAll() interprets the argument as a regular expression.The \ is an escape character in both String and regex.You need to double-escape it for regex: string.replaceAll("\\", "\\\\"); But you don’t necessarily need regex for this, simply because you want an exact character-by-character replacement and you don’t need patterns here. If an escape option is false, then the corresponding escape sequence will be treated like any other backslash before a non-escape character. This function makes a string portable, so it can be transmitted across any network to any computer that supports ASCII characters. For the second one, you need to return an IHtmlString to tell Razor that the string is already properly escaped and doesn’t require additional escaping. The resulting helper looks like this: Using the Escape Character (\) We can use the backslash (\) escape character to prevent JavaScript from interpreting a quote as the end of the string. The sequences \\, \' and \" are used to escape the character that follows the backslash. Is it possible to escape backslash using regular expression because "replace function " does not seem to make a difference in output. The backslash (\) escape character turns … A code point (also known as “character code”) is a numerical representation of a specific Unicode character. Additionally, the sequence \0, when not followed by a digit between 0 and 7, can be used to escape the null character (U+0000).. Use encodeURI() or encodeURIComponent() instead. The escape() function was deprecated in JavaScript version 1.5. Escapes or unescapes a JavaScript string removing traces of offending characters that could prevent interpretation. While similar to non-escape sequences, where the leading backslash is simply ignored (i.e. The following characters are reserved in JavaScript and must be properly escaped to be used in strings: Horizontal Tab is replaced with \t; Vertical Tab is replaced with \v Another essential PHP function that comes in handy when passing data to JavaScript is addslashes which will add a backslash before: backslashes, single- and double-quotes. The solution to avoid this problem, is to use the backslash escape character.. The backslash will be … JavaScript String Escape / Unescape. Character codes, code points, and code units. [^1] A character can be: For the first one, you just need to go through the input string and replace each character which needs escaping by a proper escape sequence. I think this can only be done by getting an unescaped ' in there. See this link please: link. For example, to echo a PHP variable into JavaScript code: The escape() function encodes a string. Is the back slash escaped or do I need to try any other way. End the script block to get into an HTML context. JavaScript also uses backslash as an escape character. While you are at it, try different numbers of backslashes, just in case. If \' is always unaffected no matter context, try \\'. Hi, I am trying to escape backslash using regular expression in javascript. \? The above code will run successfully, because the browser encounters the backslash, it knows not to try to interpret the next character.