开发者

Replace all "&gt;"(>) which are inside <code> tags

开发者 https://www.devze.com 2023-01-02 16:53 出处:网络
How can I replace all &gt; with > and &lt; with < inside a <code> tag with ruby? For example:

How can I replace all &gt; with > and &lt; with < inside a <code> tag with ruby?

For example:

<code>&lt;script&am开发者_Python百科p;gt;`alert('I steal cookies');`&lt;/script&gt;</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>"
0

精彩评论

暂无评论...
验证码 换一张
取 消