0

I am getting a HTML formatted string with tags. But there is an inconstancy with "<br>" tag. I am getting"<br>" tag in random formats which causes some extra space in text. So is there any solution to trim these multiple "<br>" tags with single one?

Example of text :-

"some text..... <br>"
"some text..... <br> <br>"   // <-- this cause some extra space.
"some text..... <br>\n"    // <-- this cause some extra space.
"some text..... <br>\n<br>" // <-- this cause some extra space.
"some text..... <br><br>\n     <br>" // <-- this cause some extra space.

So in short, there can be multiple "<br>" tags with white spaces and \n character in it.

I need to replace that with single "<br>" tag. Can anyone help me with regex for the same?

Note:- I don't want to replace any other string in between two "<br>" tags except white space and "\n" .

Code written till now -

NSMutableString *temp = [[NSMutableString alloc] initWithString:html];

            [temp replaceOccurrencesOfString:@"<br><br>" withString:@"<br>" options:0 range:NSMakeRange(0, [temp length])];
            [temp replaceOccurrencesOfString:@"<br>\n<br>" withString:@"<br>" options:0 range:NSMakeRange(0, [temp length])];
            [temp replaceOccurrencesOfString:@"<br>\n" withString:@"<br>" options:0 range:NSMakeRange(0, [temp length])];

It's a static string, needed regex for it.

1
  • one option can be obtaining the string between two <br> tags and trimming the string with \n and space. If the trimmed string is empty you can replace it altogether. Commented Oct 15, 2015 at 7:05

1 Answer 1

2

101 Regex Demo

Find:    (<br>)(\s*\1)+
Replace: $1
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.