How to convert multiple <br/> tags to a single <br/> tag using RegEx in Node.js

1 Answer

0 votes
const input = "ab<br/><br/>cd<br/>efg<br/><br/><br/>hijk<br/>lmnop<br/><br/><br/>";

// Regex pattern to match multiple consecutive <br/> tags
const pattern = /(<br\s*\/?>\s*)+/gi;

// Replace with a single <br/>
const output = input.replace(pattern, "<br/>");

console.log(output);


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

*/

 



answered Jul 15, 2025 by avibootz
...