为什么使用正则表达式进行 javascript 字符串替换会删除“/”来自我的 br 标签

发布于 2024-10-27 20:26:05 字数 602 浏览 1 评论 0原文

我使用 javascript 和一个超级简单的正则表达式来替换“<”及其 HTML 字符代码,这样我就可以使用 pre 和 code 标签在我的网站上放置一些代码并自动完成。

基本上我想弄清楚为什么这个js代码:

var str = document.getElementById("cleanme").innerHTML;
str=str.replace(/</g,"&lt;");
document.getElementById("cleanme").innerHTML = str;  

删除“/ 在 br 标签中

<pre><code id="cleanme">
<p><br />this is some code</p>
</code></pre>

没什么大不了的,因为我只是显示代码,但我仍然想知道。

它输出这个:

<p><br>this is some code</p>

谢谢

I'm using javascript with a super simple regex to replace a "<" with the HTML character code for it so I can place some code on my site using the pre and code tags and have it done automatically.

jsFiddle link

basically I'm trying to figure out why this js code:

var str = document.getElementById("cleanme").innerHTML;
str=str.replace(/</g,"<");
document.getElementById("cleanme").innerHTML = str;  

removes the "/" in the br tag

<pre><code id="cleanme">
<p><br />this is some code</p>
</code></pre>

not a huge deal because I'm just displaying code, but I'd still like to know.

it outputs this:

<p><br>this is some code</p>

thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

柠檬色的秋千 2024-11-03 20:26:05

我相信这与某些浏览器返回innerHTML 属性的方式有关。如果您使用 Google Chrome,请检查任何 << br/>使用调试工具标记,您会发现它们不显示反斜杠。当Chrome返回innerHTML属性时也是如此,黑斜杠被去掉。

因此,当您传入:

<pre><code id="cleanme">
<p><br />this is some code</p>
</code></pre>

浏览器返回一个 innerHTML 属性:

<pre><code id="cleanme">
<p><br>this is some code</p>
</code></pre>

您的正则表达式不是问题。

I believe it has to do with the way certain browsers return the innerHTML property. If you use Google Chrome, inspect any < br/ > tag using the debugging tools and you'll notice they don't show a backslash. The same is true when Chrome returns an innerHTML property, the blackslash is stripped out.

So when you pass in:

<pre><code id="cleanme">
<p><br />this is some code</p>
</code></pre>

The browser return an innerHTML property of:

<pre><code id="cleanme">
<p><br>this is some code</p>
</code></pre>

Your RegEx is not the issue.

仄言 2024-11-03 20:26:05

你的脚本没问题。

如果您尝试这样做:

var str = '<p><br />this is some code</p>';
str=str.replace(/</g,"<");
str=str.replace(/>/g,">");
document.getElementById("cleanme").innerHTML = str;

它将正确打印

可能是浏览器 HTML 规范化的影响。

Your script is OK.

If you try this:

var str = '<p><br />this is some code</p>';
str=str.replace(/</g,"<");
str=str.replace(/>/g,">");
document.getElementById("cleanme").innerHTML = str;

It'll correctly print <br />.

Possibly it's effect of browser's HTML normalization.

浅忆流年 2024-11-03 20:26:05

也许为时已晚,无法帮助您,并且您已经接受了正确的答案,但还有另一个潜在的大问题。
我在 Linux 上的 Firefox 3.6.11 和 Windows 上的 Firefox 3.6.12 上进行了尝试,它们的行为相同 --

我没有看到

这是一些代码

在你的小提琴的结果窗格中,相反,我只看到这是一些代码,根本没有标记。

通过添加 debugger; 语句作为 JavaScript 窗格中的第一行并跟踪它来向其抛出 firebug,我发现 str 正在获取 ' 值\n',也就是说,仅从 innerHTML 返回换行符,没有其他任何内容。

考虑到这一点,但无法确认这一点,我怀疑这是因为 Firefox 构建 DOM 树的方式与您期望的不同,因为您使用的 HTML 无效。行内元素不允许包含块元素;具体来说, 标签不允许包含

标签,并且

 也同样不允许包含 

标记 - 同样,在

 标记内只能使用有限的内联元素。

我认为 FF 在打开段落之前隐式关闭代码块,因此 id="cleanme" 的innerHTML只不过是换行符。它按照您的预期使用“pre”字体呈现,因为您已将浏览器置于 Quirks 模式。

Maybe too late to help you, and you've accepted a correct answer, but there's another big potential problem.
I tried this with Firefox 3.6.11 on Linux and 3.6.12 on Windows and they both behaved the same --

I did not see the <p><br>this is some code</p> in the Result pane on your fiddle, instead I saw simply this is some code with no markup at all.

Throwing firebug at it by adding a debugger; statement as the first line in the JavaScript pane and tracing through it, I found that str was getting a value of '\n', that is, just a newline was being returned from innerHTML and nothing else.

Thinking about this, but with no way to confirm it, I suspect it's because Firefox is building the DOM tree differently than you expect, because the HTML you're using is invalid. Inline elements are not allowed to contain block elements; specifically, the <code> tag is not allowed to contain a <p> tag, and <pre> is likewise not allowed to contain a <p> tag -- again, only limited inline elements can be used inside a <pre> tag).

I think FF is implicitly closing the code block before opening the paragraph so the innerHTML of id="cleanme" is nothing but the newline. It renders with the "pre" font as you expect because you've thrown the browser into Quirks Mode.

陌上芳菲 2024-11-03 20:26:05

innerHTML 不返回文字源代码,而是返回浏览器解释它的结果。

不同的浏览器将为innerHTML返回非常不同的结果,有时会省略一些引号和“可选”结束标签,将一些标签名称和属性大写,并折叠额外的空格。

HTML 不会关闭不能有结束标签的开放标签,因此它们也不被包含在内。

innerHTML does not return the literal source code, but the result of the browser's interpretation of it.

Different browsers will return very different results for innerHTML, sometimes omitting some quotes and 'optional' end tags, capitalizing some tag names and attributes, and collapsing extra white-space.

And HTML does not close open tags that can't have end tags, so they are not included either.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文