NIUCLOUD是一款SaaS管理后台框架多应用插件+云编译。上千名开发者、服务商正在积极拥抱开发者生态。欢迎开发者们免费入驻。一起助力发展! 广告
## 问题 Problem 你需要使用字符实体来替换HTML标签: You need to replace HTML tags with named entities: `<br/> => &lt;br/&gt;` ## 办法 Solution ~~~ htmlEncode = (str) -> str.replace /[&<>"']/g, ($0) -> "&" + {"&":"amp", "<":"lt", ">":"gt", '"':"quot", "'":"#39"}[$0] + ";" htmlEncode('<a href="http://bn.com">Barnes & Noble</a>') # => '&lt;a href=&quot;http://bn.com&quot;&gt;Barnes &amp; Noble&lt;/a&gt;' ~~~ ## 讨论 Discussion 可能还有比上面方法更好的实现。 There are probably better ways to implement the above method.