Regular expression - add target = "blank" to all the <a> tag links in my content
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
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