Replace all ">"(>) which are inside <code> tags
How can I replace all >
with >
and <
with <
inside a <code>
tag with ruby?
For example:
<code><script&am开发者_Python百科p;gt;`alert('I steal cookies');`</script></code>
With:
<code><script>alert('I steal cookies);<script><code>
The reason for this is because the h()
method escapes all the < and >
.
The reason for this is because the h() method escapes all the
<
and>
The method works correctly, the tags should be escaped. Why do you want to unescape them?
If you don’t want to escape the output (because you actually want to output tags), simply don’t call h()
.
If you don’t have control over the calling of h()
, then you may mark your string as “HTML safe” by calling the appropriate method on your string before passing it to h()
. But it’s really hard to say if that’s appropriate for you:
s = "<strong>example</strong>".html_safe
h(s) # = "<strong>example</strong>"
Or:
s = "<strong>example</strong>"
s.html_safe!
h(s) # = "<strong>example</strong>"
精彩评论