如何使用ColdFusion中的正则表达式将所有锚标签替换为不同的锚点 [英] How to replace all anchor tags with a different anchor using regex in ColdFusion

查看:325
本文介绍了如何使用ColdFusion中的正则表达式将所有锚标签替换为不同的锚点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里找到了一个类似的问题:使用Coldfusion在带有href标记的字符串中封装URL



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

 < p> Terminator Genisys是即将到来的2015美国科幻动作片由Alan Taylor导演。要了解详情,请点击< a href =http://www.imdb.com>点击此处< / a>< / p> 

我想做的是替换< a href = > 部分使用新版本,如下所示:

  ... 
< a href =http://www.imdb.com =nofollow noreferrer>点击此处< / a>

所以我只是添加文本 rel =nofollow noreferrer到标签。



我必须将包含href属性的锚标签与URL匹配,而不仅仅是URL字符串本身,可以这样做:

 < p> Terminator Genisys是即将到来的2015年美国科幻动作片,由Alan Taylor导演。您可以通过< a href =http://www.imdb.com> http://www.imdb.com< / a>< / p> 

在这种情况下,我仍然只想替换标签。我不想触摸实际的锚文本,即使它是一个URL。



那么如何重写这个Regex

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

解决方案

如果你愿意,这是jQuery(客户端)非常简单的任务



JSFiddle: http://jsfiddle.net/mz1rwo0u/

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

(如果您右键单击任何imdb链接和请注意,查看源将不会反映这些更改,但 >



如果你想实现每一个链接,你可以这样做。

  $(document).ready(function(){
$( $ b $(this).attr('rel','nofollow noreferrer');
});
});

最后,您也可以使用选择器来缩小范围,您可以将内容加载到dom元素,ID为 contentSection 。您可以...

  $(document).ready(function(){
$(#contentSection a)。每个(函数(e){
if($(this).attr('href')。match(/ ^ https?:\ / \ / \.com / i)){
$(this).attr('rel','nofollow noreferrer');
}});
});






融合没有意外添加它的可能性两次(没有调用一个工具像jSoup),但jQuery版本是客户端和工作通过从DOM获取数据,而不是尝试热线到它(一个jSoup实现工作类似,创建一个DOM类结构,你可以使用)。



当谈到客户端和服务器端,你必须考虑没有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 ))){
link.attr(,nofollow noreferrer);
}
}
writeOutput(htmldocument);
< / cfscript>


I found a similar question here: Wrap URL within a string with a href tags using Coldfusion

But what I want to do is replace tags with a slightly modified version AFTER the user has submitted it to the server. So here is some typical HTML text that the user will submit to the server:

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

What I want to do is replace the <a href=""> part with a new version which would be like this:

...
<a href="http://www.imdb.com" rel="nofollow noreferrer">clicking here</a>

So I'm just adding the text rel="nofollow noreferrer" to the tag.

I must match anchor tags that contain a href attribute with a URL, not just the URL string itself, because sometimes a user could just do this:

<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">http://www.imdb.com</a></p>

In which case I still only want to replace the tag. I don't want to touch the actual anchor text used even though it is a URL.

So how could I rewrite this Regex

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

the other way round, where its selecting tags and replacing them with my modified text?

解决方案

If you're willing, this is a really easy task for jQuery (client-side)

JSFiddle: http://jsfiddle.net/mz1rwo0u/

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

(If you right click any of the imdb links and Inspect Element, you'll see the rel attribute is added to the imdb links. Note that View Source won't reflect the changes, but Inspect Element is the important part.)

If you want to effect every a link, you can do this.

$(document).ready(function () {
$("a").each(function(e) {
    $(this).attr('rel','nofollow noreferrer');
});
});

Finally, you can also use a selector to narrow it down, you might have the content loading into a dom element with the id contentSection. You can do...

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


It's a bit tougher to reliably parse this in cold fusion without the possibility of accidentally adding it twice (without invoking a tool like jSoup) but the jQuery version is client-side and works by obtaining data from the DOM rather than trying to hot-wire into it (a jSoup implementation works similarly, creating a DOM-like structure you can work with).

When talking about client-side vs server-side, you have to consider the mythical user who doesn't have javascript enabled (or who turns it off with malicious intent). If this functionality is not mission-critical. I'd use JQuery to do it. I've used similar functionality to pop an alert box when the user clicks an outside link on one of my sites.

Here's a jSoup implementation, quick and dirty. jSoup is great for how it selects similarly to 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>

这篇关于如何使用ColdFusion中的正则表达式将所有锚标签替换为不同的锚点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆