-2

I have this string:

<ol>
    <li><strong>Pro</strong>Something is written here.</li>
    <li>Another one here.</li>
</ol>

I need to convert just the contents of the <li></li> to an array, if at all possible.

Suggestions?

Barmar
  • 741,623
  • 53
  • 500
  • 612
Brandon Durham
  • 7,096
  • 13
  • 64
  • 101
  • 1
    [H̸̡̪̯ͨ͊̽̅̾̎Ȩ̬̩̾͛ͪ̈́̀́͘ ̶̧̨̱̹̭̯ͧ̾ͬC̷̙̲̝͖ͭ̏ͥͮ͟Oͮ͏̮̪̝͍M̲̖͊̒ͪͩͬ̚̚͜Ȇ̴̟̟͙̞ͩ͌͝S̨̥̫͎̭ͯ̿̔̀ͅ](https://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags) – ctwheels Jan 04 '18 at 19:27
  • I’m getting eaten alive for this one. – Brandon Durham Jan 04 '18 at 19:30
  • Yes, unfortunately... If it's any consolation I haven't downvoted lol... The question seems a little broad and you don't show what you've tried, probably why your question is suffering – ctwheels Jan 04 '18 at 19:30
  • Possible duplicate of [Get all LI elements in array](https://stackoverflow.com/questions/4019894/get-all-li-elements-in-array) – ctwheels Jan 04 '18 at 19:32
  • 1
    Don't use regexp to parse HTML, use a DOM parser. – Barmar Jan 04 '18 at 19:46
  • To be clear, I never mentioned regex in my post. Only added as a tag because I thought it might be used, but it wasn’t a requirement. – Brandon Durham Jan 04 '18 at 20:07

1 Answers1

0

You could use a DOMParser.

For example:

var parser = new DOMParser();
var html = `<ol>
<li><strong>Pro</strong>Something is written here.</li>
<li>Another one here.</li>
</ol>`;
var doc = parser.parseFromString(html, "text/html");
var listItems = doc.querySelectorAll("ol li");
var arr = [];

for (var i = 0; i < listItems.length; i++) {
    arr.push(listItems[i].innerHTML);
}
console.log(arr);
The fourth bird
  • 154,723
  • 16
  • 55
  • 70