Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Regex match text between ul class

I have following html in a string i want to filter out just ul class mocha-stats in new variable how can i do that I have attached output for the same.

    var a = `<html>
            <head>
            </head>
            <body>
                <div id="mocha">
        
                    <div class="mocha-header">
                        <ul class="mocha-menu">
                            <li><span id="toggle-passes" class="toggle-passes">show failures only</span></li>
                        </ul>
                        <ul class="mocha-stats">
                            <li class="passes">passes: <em>6</em></li>
                            <li class="pending">pending: <em>1</em></li>
                            <li class="failures">failures: <em>1</em></li>
                            <li class="duration">duration: <em>15.3s</em></li>
                        </ul>
                        </ul>
                    </div>
        <div class = "b">
        </div>
        </html>`;
        
        
    var result = a.match(/<ul class="mocha-stats >(.*?)<\/ul>/g).map(function(val){
       return val.replace(/<\/?ul>/g,'');
});

output

var b =`         ` 
                <ul class="mocha-stats">
                    <li class="passes">passes: <em>6</em></li>
                    <li class="pending">pending: <em>1</em></li>
                    <li class="failures">failures: <em>1</em></li>
                    <li class="duration">duration: <em>15.3s</em></li>
                </ul>`;

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

If you have a DOM, do NOT use regexp

var a = `<html>
<head>
</head>
<body>
  <div id="mocha">
    <div class="mocha-header">
      <ul class="mocha-menu">
        <li><span id="toggle-passes" class="toggle-passes">show failures only</span></li>
      </ul>
      <ul class="mocha-stats">
        <li class="passes">passes: <em>6</em></li>
        <li class="pending">pending: <em>1</em></li>
        <li class="failures">failures: <em>1</em></li>
        <li class="duration">duration: <em>15.3s</em></li>
      </ul>
      </ul>
    </div>
    <div class="b">
    </div>
  </body>    
</html>`;
const fragment = document.createElement("div");
fragment.innerHTML = a;
const ul = fragment.querySelector(".mocha-stats");
console.log(ul.outerHTML)
const vals = [...ul.querySelectorAll("li em")]
  .map(em => ({[em.closest("li").className] : parseFloat(em.textContent)}));
console.log(vals)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading