简体   繁体   中英

How can I display a backslash in a browser using javascript?

as mentioned below, I have some code in my jsp inside a script tag .

I am getting this: 444444444666666666666666666\\888888888888

but I want this: 444444444\\666666666666666666\\\\888888888888
(The backslash should be escaped)

So how can i avoid this and display the text as it is? I have tried different ways to replace the backslash("\\") but I have been unsuccessful.

 <script>
      var mytxt ="444444444\666666666666666666\\888888888888";
  document.write(mytxt);   
 </script>

  Actual O/P in browser : 444444444666666666666666666\888888888888
  Expected O/P in browser : 444444444\666666666666666666\\888888888888

Escape each backslash with another backslash:

444444444\\666666666666666666\\\\888888888888

When you want to represent a single \\ , use \\\\ .

将反斜杠"\\"替换为"\\\\"

As you already figured out the backslashes are an escape sign. If you want to print out your backslashes then you have to print it twice.

Trying a simple replace probably won't work, because its a special character.

You should escape backslash character. \\\\ is one backslash \\\\\\\\ are two.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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