uksz uksz - 1 year ago 42
Javascript Question

How to change multiple string occurances into one?

I have a string that I receive from backend, like this:

Hello wolrd <br> <br> <br> I am <br><br> Uksz <br>.

Now, the amount of
is never constant - there can be one, or up to 5. How can I change all
that are next to each other with only one
? So that the above example would be:

Hello wolrd <br> I am <br> Uksz <br>.

Answer Source

you can use this solution:

var test = "Hello wolrd <br><br> <br> <br> <br> <br><br> I am <br><br> Uksz <br><br> <br><br> <br> <br> <br><br> <br> <br> <br><br><br>.";
var re = /(\<br\>)(\s*(\<br\>))+/gi;
var result = test.replace(re, "<br>");