Skip to content

JavaScript escape backslash | Example code

  • by

The backslash() is an escape character in JavaScript. The backslash \ is reserved for use as an escape character in JavaScript.

To escape the backslash in JavaScript use two backslashes.

\\

Escaping backslash JavaScript through regex example

HTML example code. You have to escape each \ to be \\:

<!DOCTYPE html>
<html>
<body>

  <script>
   let str = 'Hello World "This" is some sample \\ Text \' ';
   console.log(str);

 </script>
</script>
</body>
</html>

Output:

JavaScript escape backslash

Escape in JavaScript

\' single quote
\" double quote
\\ backslash
\n newline
\r carriage return
\t tab
\b backspace
\f form feed

Do comment if you have any doubts or suggestions on this JS escape code.

Note: The All JS Examples codes are tested on the Firefox browser and the Chrome browser.

OS: Windows 10

Code: HTML 5 Version

Leave a Reply

Your email address will not be published. Required fields are marked *