Note that there are some explanatory texts on larger screens.

plurals
  1. POWhat's the best way to remove HTML from a string?
    primarykey
    data
    text
    <p>I recently started using the following RegEx in a ReReplace() function to strip HTML tags from a string using ColdFusion. <strong>Please note: I am not using this as protection from XSS or SQL injection;</strong> this is only to remove existing and safe HTML from a string before it's displayed in an HTML title attribute.</p> <pre><code>REReplaceNoCase(str,"&lt;[^&gt;]*&gt;","","ALL") </code></pre> <p>In a semi-related question <a href="https://stackoverflow.com/questions/4540036/how-can-i-combining-the-following-regexs-into-one-expression">I asked how to modify my RegEx</a> to include spaces and line breaks. I was told that using RegEx for this purpose is not appropriate and <a href="https://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454">this post was referenced as an explanation.</a></p> <blockquote> <p>I strongly suspect though that the regular expressions you have posted don't in fact work correctly. I'd advise you not to use regular expressions to parse HTML as HTML is not a regular language. Use an HTML parser instead. (<a href="https://stackoverflow.com/questions/4540036/how-can-i-combining-the-following-regexs-into-one-expression/4540050#4540050">Mark Byers</a>)</p> </blockquote> <p>If this is true, what is the appropriate tool for removing HTML from a string before it's displayed? (Baring in mind the HTML is already safe; it's sanitized before entry to the DB).</p> <p>I am aware of <a href="http://help.adobe.com/en_US/ColdFusion/9.0/CFMLRef/WSc3ff6d0ea77859461172e0811cbec22c24-7847.html" rel="nofollow noreferrer">HTMLEditFormat()</a> and <a href="http://help.adobe.com/en_US/ColdFusion/9.0/CFMLRef/WSc3ff6d0ea77859461172e0811cbec22c24-7848.html" rel="nofollow noreferrer">HTMLCodeFormat()</a>, but those two functions do not provide what I need; the earlier replaces special characters with their HTML-escaped equivalents, while the latter does exactly the same but also wraps the string a <code>&lt;pre&gt;</code> tag.</p> <p>What I would like to do is clean a string from HTML and line breaks before I display in an HTML title attribute <code>&lt;a title="My string without HTML goes here"&gt;...&lt;/a&gt;</code> </p> <p>There are times when the HTML is not necessary. Say you wanted to display an excerpt from a post without the HTML stored along with it, for instance.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload