The solution to avoid this problem, is to use the backslash escape character.. I think this can only be done by getting an unescaped ' in there. 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 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. [^1] A character can be: If \' is always unaffected no matter context, try \\'. for ? Additionally, the sequence \0, when not followed by a digit between 0 and 7, can be used to escape the null character (U+0000).. 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 Hi, I am trying to escape backslash using regular expression in javascript. Is it possible to escape backslash using regular expression because "replace function " does not seem to make a difference in output. Escaping Double and Single Quotes. The resulting helper looks like this: The sequences \\, \' and \" are used to escape the character that follows the backslash. It does not show backslash in the console but it does in the return value. 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. While similar to non-escape sequences, where the leading backslash is simply ignored (i.e. JavaScript also uses backslash as an escape character. This is because Javascript strings are UTF-16, which actually sees these as two characters ("".length === 2). While you are at it, try different numbers of backslashes, just in case. Use encodeURI() or encodeURIComponent() instead. The backslash will be … 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 backslash (\) escape character turns … This function makes a string portable, so it can be transmitted across any network to any computer that supports ASCII characters. See this link please: link. 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. Using the Escape Character (\) We can use the backslash (\) escape character to prevent JavaScript from interpreting a quote as the end of the string. \? End the script block to get into an HTML context. 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. If an escape option is false, then the corresponding escape sequence will be treated like any other backslash before a non-escape character. 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. A code point (also known as “character code”) is a numerical representation of a specific Unicode character. Switch to a general JavaScript context. The string will be chopped to "We are the so-called ". JavaScript String Escape / Unescape. The escape() function was deprecated in JavaScript version 1.5. If you are lucky, nothing is done about the first backslash. Character codes, code points, and code units. For example, to echo a PHP variable into JavaScript code: Escapes or unescapes a JavaScript string removing traces of offending characters that could prevent interpretation. 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 escape() function encodes a string. Is the back slash escaped or do I need to try any other way. The above code will run successfully, because the browser encounters the backslash, it knows not to try to interpret the next character.