Skip to content Skip to sidebar Skip to footer

How To Change Multiple String Occurances Into One?

I have a string that I receive from backend, like this: Hello wolrd I am Uksz . Now, the amount of is ne

Solution 1:

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>");
console.log(result);

Solution 2:

Try this solution:-

varstring = "Hello world <br> <br> <br> I am <br><br> Uksz <br>.";
var result = string.replace(/(\<br\>\s*)+(\<br\>)/g, '$2'); // your result string will return Hello world <br> I am <br>  Uksz <br>.console.log(result); 

This regex will remove the space in between br tags only.

Solution 3:

This should work:

var yourString = "Hello wolrd <br>   <br> <br> I am <br><br> Uksz <br>.";
var pattern = /(<br>((\s)*(<br>))*)/gi;
console.log(yourString.replace(pattern, "<br>")); // returns "Hello wolrd <br> I am <br> Uksz <br>."

Note: this leaves whitespaces intact (before and after <br>).

Post a Comment for "How To Change Multiple String Occurances Into One?"