见VKS answer。它应该可以完美运行。
--- 下面是我的劣质答案:) ---
这应该做你想做的事。您可能遇到的唯一问题是链接标签的任何属性都包含“>”。
<cfset mystring = 'This is some text. It is true that <a href="http://www.cnn.com\">Harry Potter</a> is a good, but <a href="gooogle_redirect.htm">Google</a> is better'>
<cfset mystring2 = ReplaceNoCase(mystring,"</a>","","ALL")>
<cfset MyReplace = ReReplaceNoCase(mystring2,"<a [^>]*>","","ALL")>
<cfoutput><pre>Original string: #mystring#
Without link: #myreplace#</pre></cfoutput>
替代方法:如果您想删除链接和链接的文本,而不仅仅是链接,这很有用。这是我一直使用的一种肮脏的解决方法。我认为也许其他语言可能会更干净,但 CF 并不完全支持正则表达式。
找到一个字符,一个你知道不包含在任何链接文本中的字符(在<A...> 和</A> 之间)。我选择了〜,但您可能需要找到另一个。将“更接近”设置为该字符。
<cfset closer="~">
<cfset mystring = 'This is some text. It is true that <a href="http://www.cnn.com\">Harry Potter</a> is a good, but <a href="gooogle_redirect.htm">Google</a> is better'>
<cfset mystring2 = ReplaceNoCase(mystring,"</a>","#closer#","ALL")>
<cfset MyReplace = ReReplaceNoCase(mystring2,"<a [^>]*>([^#closer#]*)#closer#","","ALL")>
<cfoutput><pre>Original string: #mystring#
Without link: #myreplace#</pre></cfoutput>
这将取代
这是一些文字。 Harry Potter确实不错,但Google更好
与
这是一些文字。确实不错,但更好
如果您希望它删除整个链接和文本,只需从上面的 ReReplaceNoCase 中取出 \1。