How To Replace Link Target using Regular Expression

private string ReplaceBlankTarget(string text)
{
    string pattern = @"<a href=""https:\/\/(?<link>[^""]+)"" target=""_blank"">";
    string substitution = @"<a href=""https:\/\/${link}"" target=""_self"">";

    RegexOptions options = RegexOptions.Multiline;

    Regex regex = new Regex(pattern, options);
    return regex.Replace(text, substitution);
}

 

Comments

Leave a Comment

All fields are required. Your email address will not be published.