So let's say I have an ordered or unordered list that is in string form. I need only to return the text that is contained within each list item of the respective parent element:
<ul>
<li>Example One</li>
<li>Example Two</li>
</ul>
I have made this work, but obviously not very efficient:
var first = string.replace(/.*<li>(.*)<\/li>.*/g, '$1');
var second = first.replace(/(<ul>|<ol>|<\/ol>|<\/ul>)/g, '');
Output is what I expect, but I know there is a regex format that will accomplish at once, but I am still pretty green in regards to regex so not sure what I am doing wrong. This is what I thought would work:
var newString = string.replace(/(<ul>|<ol>).*<li>(.*)<\/li>.*(<\/ul>|\/<ol>)/g, '$2');
However, this returns the entire HTML structure as a string:
<ul>
<li>Example One</li>
<li>Example Two</li>
</ul>
As always my friends, thank you in advance.
` perhaps regex is the wrong way.
or
that is in string form and set to server-side vars to subsequently rendered in a template as DOM elements
- Example One
- Example Two
World` as input fails the test because Hello and World shows. – GetSet Apr 09 '20 at 07:50or
and end with a or
...although if I am still mistaken I am very much appreciative of your input as it is already helping me to have a better understanding of regex – Austin737 Apr 09 '20 at 08:00