I tried to render wiki syntax. The following affects ordered lists. The tasks are:
- Find a block of lines, which belong to a list to render a
ol. - Render for each line a
li.
I wrote the following regular expression for a block:
(?<item>^#[^]+?$\n?)+
The expression for a block is just a sequence of the regular expression of an item.
Problem: when I execute the regular expression, it captures only the last element of the sequence.
Question: is there any way to get all items out of the regular expression engine?
Of course I can write another regular expression, which captures only one item instead of a sequence of items, in order to execute it repetitively until I have all items. But this would duplicate the work, which has already been done by the regular expression for the sequence of items. And duplicating work sounds stupid to me.
var x = document.getElementById('x');
var y = document.getElementById('y');
var rx_ol = new RegExp ('(?<item>^#[^]+?$\n?)+', 'gm');
function render_ol (match, a1, a2, a3, a4)
{
console.log ('a1', a1);
console.log ('a4', a4);
return `<ol>${match}</ol>`;
}
y.textContent = x.textContent.replace (rx_ol, render_ol);
pre {
border: 1px solid black;
padding: 1ex;
}
<pre id="x">
A
# X
# Y
B
</pre>
<pre id="y">
</pre>