How to convert multiple <br/> tags to a single <br/> tag using RegEx in Kotlin

1 Answer

0 votes
fun main() {
    var input = "ab<br/><br/>cd<br/>efg<br/><br/><br/>hijk<br/><br/>"

    // Replace one or more <br/> tags with a single <br/>
    input = Regex("(<br\\s*/?>\\s*)+").replace(input, "<br/>")

    println(input)
}

  
 
  
/*
run:
 
ab<br/>cd<br/>efg<br/>hijk<br/>

*/

 



answered Jul 15, 2025 by avibootz
...