最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

java - How can I display a backslash in a browser using javascript? - Stack Overflow

programmeradmin4浏览0评论

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

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
Share Improve this question edited Nov 16, 2011 at 8:40 Matt Hulse 6,2324 gold badges30 silver badges37 bronze badges asked Nov 16, 2011 at 7:38 SatyaSatya 8,34610 gold badges42 silver badges44 bronze badges 1
  • I used mytxt = mytxt.replace('\\',"\\\\"); to replace the backslash("\") but no change in output – Satya Commented Nov 16, 2011 at 8:41
Add a ment  | 

4 Answers 4

Reset to default 8

Escape each backslash with another backslash:

444444444\\666666666666666666\\\\888888888888

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

Replace backslash "\" with "\\"

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

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.

发布评论

评论列表(0)

  1. 暂无评论