Javascript: replace all instances that appear between multiple sets of brackets

I have a line with several sets of brackets, and each set of brackets contains several <br> tags. For example, one fragment can read:

 <p>some text<br>[text<br> text text<br>text]<br>some more text<br>[text]</p> 

I need to remove <br> tags in brackets, but not <br> tags outside of brackets. I tried to do this with a .replace and .indexOf () loop, but it removed the <br> tags outside the brackets. Any ideas? Thanks!

+4
source share
1 answer

You can use the regular expression and replace it when it matches <br> in brackets:

 var regexp = /\[(.*)<br>(.*)\]/; // [anything<br>anything] while(regexp.test(str)) { str = str.replace(regexp, "[$1$2]"); // replace with [anythinganything] } 
+5
source

Source: https://habr.com/ru/post/1400260/