【问题标题】:Redirecting external links to an intermediate page in c#在c#中将外部链接重定向到中间页面
【发布时间】:2015-01-26 03:43:21
【问题描述】:

我正在使用以下代码来识别源 html 并附加中间页面的 url。

 sourceHtml = Regex.Replace(sourceHtml,
            @"((http|ftp|https):\/\/[\w\-_]+(\.[\w\-_]+)+([\w\-\.,@?^=%&:/~\+#]*[\w\-\@?^=%&/~\+#])?)","http://domain.com/out/?url=$1");

虽然这段代码运行良好,但我面临两个问题,

1.将'a'标签的内容(与href值相同)替换为中间页面链接。

2.它还附加了“img”标签的url。

请帮助我将此正则表达式仅应用于 href 值。

【问题讨论】:

    标签: regex asp.net-mvc


    【解决方案1】:

    尝试用这个替换你的正则表达式:

    href\=\""((http|ftp|https):\/\/[\w\-_]+(\.[\w\-_]+)+([\w\-\.,@?^=%&:/~\+#]*[\w\-\@?^=%&/~\+#])?)\""
    

    它将仅替换 href 值。

    【讨论】:

    • 您好,您可以修改我的代码并在此处发布。我试过了,但不确定我是否做得对
    • 以前我只是在正则表达式测试器上做的。这是在 c# sourceHtml = Regex.Replace(sourceHtml, @"href\=""((http|ftp|https):\/\/[\w\-_]+(\.[\w\-_]+)+([\w\-\.,@?^=%&:/~\+#]*[\w\-\@?^=%&/~\+#])?)""", "href=\"http://domain.com/out/?url=$1\""); 中与您的案例一起使用的案例
    猜你喜欢
    • 2022-06-18
    • 1970-01-01
    • 1970-01-01
    • 2016-03-10
    • 1970-01-01
    • 2014-10-06
    • 1970-01-01
    • 1970-01-01
    • 2017-11-18
    相关资源
    最近更新 更多