Replacing HTML Tags with HTML Named Entities
Problem
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.