-1

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>');
Kiran
  • 896
  • 1
  • 6
  • 25
  • 2
    https://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454 – Quentin Feb 01 '18 at 13:14
  • Please share your input and desired output. Your question will be marked as unclear if you don't share that info. – Ele Feb 01 '18 at 13:15

1 Answers1

1

It is not a good idea to parse HTML using regex, you're better off using something like jQuery.

Here is an example:

var str = "abcd <div>div</div> abcd <div>div with <br></div> <div>abcd div with <p> abcd</div>";
var $e = $("<div></div>");
$e.html(str);
var result = $e.find("div").map(function(){
    return $(this).html();
}).get().join(" ");
console.log(result);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
Titus
  • 22,031
  • 1
  • 23
  • 33