当我尝试运行以下代码时,它不会将 <、>、" 替换为其等效字符
<% if(typeof myTable !== 'undefined'){ %>
<% myTable = myTable.replace('<','<').replace('>', '>').replace('"', '"') %>
<script>
localStorage['text'] += "<%= myTable %>";
</script>
<% myTable = undefined; %>
<% } %>
当我检查 myTable 的值(替换为 ejs 的 plceholder 标记)时,它仍然具有原始形式的这些字符。
myTable = "<table class="table table-bordered table-hover"><thead><tr><th class="col"><p contenteditable="true">Head</p></th><th class="col"><p contenteditable="true">Head</p></th></tr></thead><tbody><tr><td><p contenteditable="true">Null</p></td><td><p contenteditable="true">Null</p></td></tr><tr><td><p contenteditable="true">Null</p></td><td><p contenteditable="true">Null</p></td></tr></tbody></table>"
我应该做什么?
最佳答案
我会尝试存储替换字符串的值
<% myTable = myTable.replace('<','<').replace('>', '>').replace('"', '"') %>
否则就丢失了。
来自docs
The replace() method searches a string for a specified value, or a regular expression, and returns a new string where the specified values are replaced.
如果这不起作用,请尝试未转义版本的蜂蜇:"<%- myTable %>";
(而不是 "<%= myTable %>";
)。
关于javascript - .replace() 在嵌入式 javascript 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52981682/