Remove consecutive <br> from string using regex c#

c#, regex

Solution

If you need to account for the case where there is whitespace between the tags, try the following regex:

myInputStr = Regex.Replace(myInputStr,
    @"([\b\s]*<[\b\s]*[bB][rR][\s]*/?[\b\s]*>){2,}",
    "<br>", RegexOptions.Multiline);

This regex will replace 2 or more instances of `<br>` tags with a single instance, regardless of the formation of the tag (spacing, casing, self-closing etc.).

Problem

I have following string regex ``` "choose to still go on the trip. <br><br>\r\nNote that when booking" ``` After converting it with regex I need to replace `<br>` tags with only one `<br>` so string would be like this ``` "choose to still go on the trip. <br>Note that when booking" ```

Original source

Related problems