My regular expression won't return all instances of <li>.+<\/li> found in textarea.
I Googled, and tried /g, but doesn't work.
Here is the Fiddle.
This is my function(){...}:
function doIt() {
var input = document.getElementById("input");
var olPatt = /<ol>\s*(?:<li>.+<\/li>\s*)+<\/ol>/,
ol = input.value.match(olPatt),
olLi = ol.toString().match(/<li>/g), // MATCH ALL <li> with /g
olLiB = ol.toString().match(/<\/li>/g); // MATCH ALL </li> with /g
var i = 1; // start with 1
input.value = input.value.replace(/<ol>/, "").replace(/<\/ol>/, "").replace(/\s*/, ""); // remove ol tags
input.value = input.value.replace(olLi, function() {return i++ + "." + " ";}).replace(olLiB, " ");
// should replace ALL <li> found in <ol> with number starting with 1 </li>
}
If you're given this:
<ol>
<li>Hello world! :)</li>
<li>Hello how are you</li>
<li>good</li>
</ol>
It returns this: (incorrect)
1. Hello world! :)
<li>Hello how are you</li>
<li>good</li>
But, I want this: (correct)
1. Hello world! :)
2. Hello how are you
3. good
{1}is redundant, and you should consider using an HTML parser (like the one that’s built into browsers).<ol>{1}is easier to read than<ol>?{1}only applies to the>. If you changed it to2later, it would match<ol>>, not<ol><ol>.