[英]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 . 如下所述,我的jsp脚本脚本中有一些代码。
I am getting this: 444444444666666666666666666\\888888888888
我得到这个:
444444444666666666666666666\\888888888888
but I want this: 444444444\\666666666666666666\\\\888888888888
但我想要这个:
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. \\\\
是一个反斜杠\\\\\\\\
是两个。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.