【问题标题】:Remove href tag along with content coldfusion删除href标签以及内容coldfusion
【发布时间】:2014-12-10 05:47:23
【问题描述】:

如何在coldfusion中删除带有完整链接的<a href标签?

我想从以下内容中删除"<a href="http://www.cnn.com">Harry Potter</a>"

This is some text. It is true that <a href="http://www.cnn.com">Harry Potter</a> is a good

我用过正则表达式"&lt;[aA].*?&gt;.*?&lt;/[aA]&gt;"

但它不起作用。

【问题讨论】:

  • #form.textcontet#
    ]*>[^]*", "", "all")> #matchData# cf输出>
  • 你遇到什么错误???如果你得到一些输出那是什么??
  • 编辑您的问题以添加代码。它将更具可读性,您将更有可能获得帮助。
  • @swetha:评论是临时的。请edit您的问题包含您的代码。

标签: regex coldfusion tags href


【解决方案1】:

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 并不完全支持正则表达式。

找到一个字符,一个你知道不包含在任何链接文本中的字符(在&lt;A...&gt;&lt;/A&gt; 之间)。我选择了〜,但您可能需要找到另一个。将“更接近”设置为该字符。

<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。

【讨论】:

    【解决方案2】:

    假设您要保留超链接之间的文本:

    <cfsavecontent variable="content">
       This is some text. It is true that <a href="http://www.cnn.com">Harry Potter</a> is a good
    </cfsavecontent>
    
    <cfset content = reReplaceNoCase(content, "(<a.*?>)(.*?)(</a>)", "\2", "all") />
    <cfoutput>#content#</cfoutput>
    

    会导致:

    这是一些文字。哈利波特确实不错

    【讨论】:

      【解决方案3】:
      <[aA][^>]*>[^<>]*<\/[aA]>
      

      试试这个。替换为empty string

      查看演示。

      http://regex101.com/r/dZ1vT6/14

      【讨论】:

      • @swetha 检查了冷融合的正则表达式。它类似。肯定有其他问题。你是如何应用它的
      • @vks 这至少在 cf9 中有效,我不明白为什么它在早期的 v 中无效。它甚至可以拾取多个链接。好工作。 &lt;cfset mystring = 'This is some text. It is true that &lt;a href="http://www.cnn.com\"&gt;Harry Potter&lt;/a&gt; is a good, but &lt;a href="gooogle_redirect.htm"&gt;Google&lt;/a&gt; is better'&gt; &lt;cfset MyReplace = ReReplaceNoCase(mystring,"&lt;[aA][^&gt;]*&gt;([^&lt;&gt;]*)&lt;\/[aA]&gt;","\1","ALL")&gt; &lt;cfoutput&gt;&lt;pre&gt;Original string: #mystring# Without link: #myreplace#&lt;/pre&gt;&lt;/cfoutput&gt; OP 如果喜欢,可以将代码中的“\1”替换为“”。干得好
      • @swetha 鉴于上面的代码示例,他的回答确实有效。在 cflive.net 上测试
      猜你喜欢
      • 1970-01-01
      • 2020-11-11
      • 1970-01-01
      • 2018-10-28
      • 2014-06-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-03
      相关资源
      最近更新 更多