Regex 如何在ColdFusion中使用正则表达式用不同的锚替换所有锚标记

Regex 如何在ColdFusion中使用正则表达式用不同的锚替换所有锚标记,regex,coldfusion,Regex,Coldfusion,我在这里发现了一个类似的问题: 但我想做的是在用户将标签提交到服务器后,用稍微修改的版本替换标签。下面是一些典型的HTML文本,用户将提交给服务器: <p>Terminator Genisys is an upcoming 2015 American science fiction action film directed by Alan Taylor. You can find out more by <a href="http://www.imdb.com">clic

我在这里发现了一个类似的问题:

但我想做的是在用户将标签提交到服务器后,用稍微修改的版本替换标签。下面是一些典型的HTML文本,用户将提交给服务器:

<p>Terminator Genisys is an upcoming 2015 American science fiction action film directed by Alan Taylor. You can find out more by <a href="http://www.imdb.com">clicking here</a></p>
在这种情况下,我仍然只想替换标签。我不想触摸实际使用的锚文本,即使它是一个URL

那么我如何重写这个正则表达式呢

#REReplaceNoCase(myStr, "(\bhttp://[a-z0-9\.\-_:~@##%&/?+=]+)", "<a href=""\1"">\1</a>", "all")#
\REReplaceNoCase(myStr,“(\bhttp://[a-z0-9\.\-”:~@##

另一种方法是,选择标记并用我修改过的文本替换它们

如果愿意,这对于jQuery(客户端)来说是一项非常简单的任务

JSFiddle:

(如果右键单击任何imdb链接和
Inspect元素
,您将看到将rel属性添加到imdb链接。请注意
查看源代码
不会反映更改,但
Inspect元素
是重要部分。)

如果你想影响每一个链接,你可以这样做

$(document).ready(function () {
$("a").each(function(e) {
    $(this).attr('rel','nofollow noreferrer');
});
});
最后,还可以使用选择器缩小范围,您可以将内容加载到id为
contentSection
的dom元素中。你可以做

$(document).ready(function () {
$("#contentSection a").each(function(e) {
  if ($(this).attr('href').match(/^https?:\/\/(www\.)?imdb\.com/i)) {
    $(this).attr('rel','nofollow noreferrer');
  }});
});

要在cold fusion中可靠地解析它,而不可能意外地添加它两次(不调用像jSoup这样的工具),这有点困难,但是jQuery版本是客户端的,通过从DOM获取数据而不是试图将数据热连接到DOM中来工作(jSoup实现的工作原理与此类似,它创建了一个类似于DOM的结构,您可以使用它)

当谈到客户端VS服务器端时,你必须考虑没有启用JavaScript的神秘用户(或者是恶意的关闭它)。。如果此功能不是任务关键型的。我会使用JQuery来完成。我使用过类似的功能,在用户单击我网站上的外部链接时弹出一个警报框

这是一个jSoup实现,快速而肮脏。jSoup在选择方面与jQuery非常相似

<cfscript>
jsoup = CreateObject("java", "org.jsoup.Jsoup");
HTMLDocument = jsoup.parse("<A href='http://imdb.com'>test</a> - <A href='http://google.com'>google</a>");

As = htmldocument.select("a");
for (link in As) {
  if (reFindnoCase("^https?:\/\/(www\.)?imdb\.com",link.attr("href"))) {
    link.attr("rel","nofollow noreferrer");
  }
}
writeOutput(htmldocument);
</cfscript>

jsoup=CreateObject(“java”、“org.jsoup.jsoup”);
HTMLDocument=jsoup.parse(“”);
As=htmldocument.select(“a”);
用于(链接为){
如果(reFindnoCase(“^https?:\/\/(www.)?imdb\.com”,link.attr(“href”)){
link.attr(“rel”、“nofollow-noreferrer”);
}
}
写输出(htmldocument);

Checkout jSoup。它非常适合这种情况。这太棒了!我是否要删除匹配项()如果我想在#contentSection div中获得所有锚定标记,那部分是因为我只使用了imbd作为示例-URL可以是任何东西。我考虑在后端使用CF的原因之一是为了防止用户没有启用JS…但我想这太过分了?是的,您只需删除
if…
。这取决于h这是一个非常关键的功能。如果它不重要,我会使用jQuery。但是,我添加了一个基本的jSoup版本。@VolumeOn举个例子,我说我使用类似于弹出一个指向站点外部链接的警报——在我的情况下,警报是一个很好的功能,但不是关键功能。我意识到我需要使用ColdFusion来完成它A而不是jQuery。原因是我添加的内容是为Google爬行机器人设计的。Google机器人没有启用JavaScript。即使我可以创建某种功能,在运行时进行替换,而不是将其存储在数据库中,也会很困难ideal@volumeoneJsoup是实现此目的所需的工具,那么cold fusion正则表达式将正如我所演示的,jSoup在服务器端进行更改,并将其交付给浏览器。
$(document).ready(function () {
$("a").each(function(e) {
    $(this).attr('rel','nofollow noreferrer');
});
});
$(document).ready(function () {
$("#contentSection a").each(function(e) {
  if ($(this).attr('href').match(/^https?:\/\/(www\.)?imdb\.com/i)) {
    $(this).attr('rel','nofollow noreferrer');
  }});
});
<cfscript>
jsoup = CreateObject("java", "org.jsoup.Jsoup");
HTMLDocument = jsoup.parse("<A href='http://imdb.com'>test</a> - <A href='http://google.com'>google</a>");

As = htmldocument.select("a");
for (link in As) {
  if (reFindnoCase("^https?:\/\/(www\.)?imdb\.com",link.attr("href"))) {
    link.attr("rel","nofollow noreferrer");
  }
}
writeOutput(htmldocument);
</cfscript>