为什么使用正则表达式进行 javascript 字符串替换会删除“/”来自我的 br 标签
我使用 javascript 和一个超级简单的正则表达式来替换“<”及其 HTML 字符代码,这样我就可以使用 pre 和 code 标签在我的网站上放置一些代码并自动完成。
”
基本上我想弄清楚为什么这个js代码:
var str = document.getElementById("cleanme").innerHTML;
str=str.replace(/</g,"<");
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.
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我相信这与某些浏览器返回innerHTML 属性的方式有关。如果您使用 Google Chrome,请检查任何 << br/>使用调试工具标记,您会发现它们不显示反斜杠。当Chrome返回innerHTML属性时也是如此,黑斜杠被去掉。
因此,当您传入:
浏览器返回一个 innerHTML 属性:
您的正则表达式不是问题。
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:
The browser return an innerHTML property of:
Your RegEx is not the issue.
你的脚本没问题。
如果您尝试这样做:
它将正确打印
。可能是浏览器 HTML 规范化的影响。
Your script is OK.
If you try this:
It'll correctly print
<br />
.Possibly it's effect of browser's HTML normalization.
也许为时已晚,无法帮助您,并且您已经接受了正确的答案,但还有另一个潜在的大问题。
我在 Linux 上的 Firefox 3.6.11 和 Windows 上的 Firefox 3.6.12 上进行了尝试,它们的行为相同 --
我没有看到
这是一些代码
在你的小提琴的结果窗格中,相反,我只看到
这是一些代码
,根本没有标记。通过添加
debugger;
语句作为 JavaScript 窗格中的第一行并跟踪它来向其抛出 firebug,我发现str
正在获取' 值\n'
,也就是说,仅从innerHTML
返回换行符,没有其他任何内容。考虑到这一点,但无法确认这一点,我怀疑这是因为 Firefox 构建 DOM 树的方式与您期望的不同,因为您使用的 HTML 无效。行内元素不允许包含块元素;具体来说,
标签不允许包含
标签,并且
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 simplythis 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 thatstr
was getting a value of'\n'
, that is, just a newline was being returned frominnerHTML
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.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.