简体   繁体   English

用JavaScript输出转义字符

[英]Outputting Escape Characters in JavaScript

I think it is a bit simple question but I couldn't find my answer neither on Stackoverflow nor Google. 我认为这是一个简单的问题,但我在Stackoverflow和Google上都找不到我的答案。 Here is my question. 这是我的问题。 I want to output strings with escape characters. 我想输出带有转义字符的字符串。 I have used the method document.getElementIdBy(). 我已经使用了方法document.getElementIdBy()。

Here is my example 这是我的例子

<!DOCTYPE html>
<html>
<head>
<title>JavaScript</title>
</head>
<body>
    <p id="example1"></p><br>
    <p id="example2"></p><br>
    <p id="example3"></p><br>
    <p id="example4"></p><br>
    <script>
        var x = "\"ABC\""
        var y = "\'ABC\'"
        var z = "ABC\nDEF"
        var t = "ABC\bDEF"
        document.getElementById("example1").innerHTML=x;
        document.getElementById("example2").innerHTML=y;
        document.getElementById("example3").innerHTML=z;
        document.getElementById("example4").innerHTML=t;
    </script>
</body>
</html>

The first two works fine. 前两个工作正常。 The third one doesn't create a new line and the fourth one doesn't crate a backspace. 第三个不创建新行,第四个不创建退格键。 I assume that the variable z is like this 我假设变量z像这样

ABC
DEF

If I write this into ap element, it must show up like this: ABC DEF. 如果我将此内容写入ap元素,则它必须显示为:ABC DEF。 Therefore I can understand why it doesn't appear as I expected (If I style the p element with white-space:pre it works as I expected) 因此,我可以理解为什么它没有按预期出现(如果我将p元素设置为white-space:pre则可以按预期工作)

However I wonder why \\b escape character doesn't work as expected. 但是我想知道为什么\\ b转义符不能按预期工作。 Actually I was expecting the output to be: ABDEF (without C). 实际上,我期望输出为:ABDEF(不带C)。 There may be some logic similar to the upper one but I cannot find. 可能有些逻辑与上层逻辑相似,但我找不到。 Can someone explain why it doesn't work? 有人可以解释为什么它不起作用吗?

I think these chars are just stripped from HTML, you could achieve what you want by replacing \\n with <br/> 我认为这些字符只是从HTML剥离的,您可以通过用<br/>替换\\ n来实现所需的功能

eg 例如

document.getElementById("example3").innerHTML=z.replace("\n","<br/>");

The third one doesn't create a new line 第三个不创建新行

New lines in text are ignored in HTML tags. HTML标记中将忽略文本中的新行。 They are rendered as a space . 它们被渲染为space Use <pre> tags to keep formatting: 使用<pre>标记保持格式:

<pre id="example3"></pre>

Or add <p> tags instead of new lines: 或添加<p>标记而不是换行:

var z = "<p>ABC</p><p>DEF</p>"

Or <br> <br>

var z = "ABC<br>DEF"

the fourth one doesn't crate a backspace 第四个不创建退格键

Do not pretty sure that HTML/JS supports \\b . 不太确定HTML / JS是否支持\\b

new line ( \\n ) doesn't generate new line in html. 新行( \\n )不会在html中生成新行。

so if you write: 所以如果你写:

<p>first line
second line</p>

you will get:first line second line. 您将获得:第一行第二行。

so to write \\n to html you must convert it to <br> . 因此要将\\n写入html,必须将其转换为<br>

document.getElementById("example3").innerHTML=z.replace(/\n/g,'<br>');

this regular expression replaces all \\n with <br> . 此正则表达式将所有\\n替换为<br>

and \\b is just character with code 8 . \\b只是带有代码8字符。 its special behavior occurs only when you send it to an input or text box. 仅当您将其发送到输入框或文本框时,才会发生其特殊行为。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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