【发布时间】:2021-10-10 09:37:27
【问题描述】:
我目前将数据库中的一个值直接加载到隐藏的文本区域中。
<textarea name="text" id="text" style="visibility:hidden">
[textarea]Content showing raw [b]HTML[/b] or any other code
Including line breaks </a>[/textarea]
</textarea>
我从那里获取 textarea 的内容并通过几个使用简单 Javascript 的替换参数运行它,例如
<script type="text/javascript">
document.addEventListener('DOMContentLoaded', function parser() {
post_text=post_text.replace(/\r?\n/g, "<br>");
post_text=post_text.replace(/\[size=1\]/g, "<span style=\"font-size:80%\">");
post_text=post_text.replace(/\[url=(.+?)\](.+?)\[\/url\]/g, "<a href=\"$1\" target=\"_blank\">$2</a> <img src=\"images/link.gif\" style=\"border:0px\">");
post_text=post_text.replace(/\[url\](.+?)\[\/url\]/g, "<a href=\"$1\" target=\"_blank\">$1</a> <img src=\"images/link.gif\" style=\"border:0px\">");
document.getElementById('vorschau').innerHTML = post_text;
}, false);
</script>
<div id="vorschau"></div>
将其呈现为 HTML,然后由浏览器解析,因此我在前端/客户端对条目进行所有格式设置。
不过,textarea 也可能包含这样的 UBB 标签:
[textarea]Content showing raw [b]HTML[/b] or any other code
Including line breaks </a>[/textarea]
我目前只是像任何其他内容一样替换 textarea UBB 元素
post_text=post_text.replace(/\[textarea\]/g, "<textarea id=\"codeblock\" style=\"width:100%;min-height:200px;\">");
post_text=post_text.replace(/\[\/textarea\]/g, "</textarea>");
问题在于我的其他代码
post_text=post_text.replace(/\r?\n/g, "<br>");
post_text=post_text.replace(/\</g, "<");
post_text=post_text.replace(/\>/g, ">");
不跳过 [textarea][/textarea] 元素中的内容,从而导致 textarea 填充以下内容:
Content showing raw <b>HTML</b> or any other code<br>Including line breaks </a>
那么如何防止替换 [textarea][/textarea] 中的任何内容(在 id="text" 中可能出现多次)?
【问题讨论】: