js转义符< >
如果需要在JS中使用含有尖括号
<erb<%%><>
比如下面的示例代码中,我们需要将一个字符串
"2 < 3"title
title: {
text: '2 <%#这里使用转义字符%>&lt; 3',
},其中
&lt;<&#60;2 < 3
在实际使用中,如果需要频繁使用含有尖括号的字符串,可以将其封装成一个方法或者变量来方便地进行调用,而不用每次都手动转义。例如:
# 将字符串"<demo>"转义为"&lt;demo&gt;"
def escape_html(str)
str.gsub(/</, "&lt;").gsub(/>/, "&gt;")
end
# 使用转义后的字符串
title: {
text: "Title with <%= escape_html("<demo>") %>",
},