tag links in my content Can someone help me create a regex in C # .net to add target="...">

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

Can someone help me create a regex in C # .net to add target="_blank"to all tags <a>in my content?

If the link already has a target set, replace it with "_blank". The goal is to open all links in my content in a new window.

Appreciate your help

-dotnet rocks

+3
source share
4 answers

There are many references to not using regex when parsing HTML , so you can use the Html Agility Pack to do 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");
    }
}

( ) target='_blank' .

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

,

+4

I did this using an extension method similar to what Alex showed. 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 clear links:

// Enforce targets for links as "_blank" to open in new window
asset.Description = asset.Description.SetHtmlLinkTargetAttribute("_blank");
0
source
RegEx.Replace(inputString, "<(a)([^>]+)>", "<$1 target=""_blank""$2>")
-1
source

Source: https://habr.com/ru/post/1744853/


All Articles