Strip out HTML tags using RegEx

This code will strip out all the HTML tags and truncate the text to 4 lines.

public static string TruncateText(string txtIn, int newLength)
{
    string txtOut = txtIn;
    string pattern = @"<(.|\n)*?>";

    //Strip out HTML tags
    if (Regex.IsMatch(txtIn, pattern, RegexOptions.None))
        txtOut = Regex.Replace(txtIn, pattern, string.Empty, RegexOptions.Multiline).Trim();

    if (txtOut.Length > newLength)
    {
        int endPos = txtOut.LastIndexOf(" ", newLength);
        txtOut = txtOut.Substring(0, endPos) + "...";
    }

    return txtOut;
}
If you liked this post, 🗞 subscribe to my newsletter and follow me on 𝕏!