I am trying to get content of all divs where the content does not contain any html tags except <br>
tags.
str.match(/<div>.*?<\/div>/g);
gives all div contents whereas str.match(/<div>([^<>])*<\/div>/g,);
gives div contents without any html tags.
I managed to get a solution. I can covert all <br>
tags to some other codes, then use str.match(/<div>([^<>])*<\/div>/g,);
and finally revert the <br>
tags. I do not know whether this is a good solution.
Is it possible using regular expression to get all div contents without any html tags except <br>
? Or a better solution exists? (preferably by not using look ahead or look behind). Thanks.
EDIT:
input string: abcd <div>div</div> abcd <div>div with <br></div> <div>abcd div with <p> abcd</div>
In this, i like to match <div>div</div>
and <div>div with <br></div>
var str = 'abcd <div>div</div> abcd <div>div with <br></div> <div>abcd div with <p> abcd</div>';
var res = str.match(/<div>.*?<\/div>/g);
document.write('<textarea>'+res+'</textarea>');
var res = str.match(/<div>([^<>])*<\/div>/g);
document.write('<textarea>'+res+'</textarea>');