开发者

Regular expression- add target="blank" to all <a> tag links in my content

Can some one help me create a regular expression in C#.net to add target="_blank" to all <a> tag links in my content?

If the link already has a target set then replace it with "_blank". The pu开发者_运维知识库rpose is to open all links in my content in a new window.

Appreciate your help

-dotnet rocks


There are a lot of mentions regarding not to use regex when parsing HTML, so you could use Html Agility Pack for this:

HtmlDocument document = new HtmlDocument();
document.LoadHtml(yourHtml);

var links = document.DocumentNode.SelectNodes("//a");
foreach (HtmlNode link in links)
{
    if (link.Attributes["target"] != null)
    {
        link.Attributes["target"].Value = "_blank";
    }
    else
    {
        link.Attributes.Add("target", "_blank");
    }
}

this will add(or replace if necessary) target='_blank' to all the anchors in your document.


RegEx.Replace(inputString, "<(a)([^>]+)>", "<$1 target=""_blank""$2>")

It will add target also in those anchor tags which already have target present


I did this with an extension method similar to the approach Alex showed. The method:

// Return the input string with all parsed HTML links having the "target" attribute set to specified value
// Links without a target attribute will have the attribute added, existing attributes values are updated
public static string SetHtmlLinkTargetAttribute(this string inputHtmlString, string target)
{
    var htmlContent = new HtmlDocument();
    htmlContent.LoadHtml(inputHtmlString);

    // Parse HTML content for links
    var links = htmlContent.DocumentNode.SelectNodes("//a");
    foreach (var link in links)
    {
        link.SetAttributeValue("target", target);
    }

    return htmlContent.DocumentNode.OuterHtml;
}

And using it to clean up my links:

// Enforce targets for links as "_blank" to open in new window
asset.Description = asset.Description.SetHtmlLinkTargetAttribute("_blank");


RegEx.Replace(inputString, "<(a)([^>]+)>", "<$1 target=""_blank""$2>")
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜