使用HTML字符实体替换HTML标签 Replacing HTML Tags with HTML Named Entities
问题 Problem
你需要使用字符实体来替换HTML标签:
You need to replace HTML tags with named entities:
<br/> => <br/>
办法 Solution
htmlEncode = (str) ->
str.replace /[&<>"']/g, ($0) ->
"&" + {"&":"amp", "<":"lt", ">":"gt", '"':"quot", "'":"#39"}[$0] + ";"
htmlEncode('<a href="http://bn.com">Barnes & Noble</a>')
# => '<a href="http://bn.com">Barnes & Noble</a>'
讨论 Discussion
可能还有比上面方法更好的实现。
There are probably better ways to implement the above method.