从纯文本中解析包含换行符的项目符号

Esw*_*ala 6 javascript regex mediawiki

我正在尝试解析包含多个项目符号的文本文档.

我想解析具有单个换行符的子弹点,但是当找到2个或更多换行符时想要中断.

for example :
-----------------------------------
* bullet
text on new line
more text

this should be a separate block
-----------------------------------

when passed through the function, this should capture :
-----------------------------------
-> start
bullet 
text on new line 
more text
<- end capture

this should be a seperate block
-----------------------------------
Run Code Online (Sandbox Code Playgroud)

这是我到目前为止,我已经编写了一个javascript函数,可以递归地解析有序/无序的mediawiki'sh列表到HTML.唯一不同的是,块在2个换行符上插入,而对于1个换行符的mediawiki方式.

function parseLists(str)
{
//How can I capture bulleted lines with less than or equal to "1" newline character? 
    return str.replace(/(?:(?:(?:^|\n)[\*#].*)+)/g, function (match) {
        var listType = match.match(/(^|\n)#/) ? 'ol' : 'ul';
        match = match.replace(/(^|\n)[\*#][ ]{0,1}/g, "$1");
        match = parseLists(match);
        return '<'
                + listType + '><li>'
                + match.replace(/^\n/, '').split(/\n/).join('</li><li>')
                + '</li></' + listType
                + '>';
    });
}
Run Code Online (Sandbox Code Playgroud)

http://jsfiddle.net/epinapala/L18y7zyx/7/

我认为问题在于第一个正则表达式 - /(?:(?:(?:^|\n)[*#].*)+)/g来匹配bullts,这个正则表达式实际上在发现换行符时会中断,如何捕获小于或等于"1"换行符的项目符号行?

我想用子线中的换行符解析子弹,并且只有在有2个或更多新换行符时才想打破子弹.其次是子弹内容.

[编辑] - 我能够进行一些更改,我的功能的当前版本如下所示

function parseLists2(str)
{
  return str.replace(/(?:(?:(?:^|\n)[\*#](?:.+\n)+.*))/g, function(match){ 
      match = match.replace(/\n(?![#\*])/g," ");
        //alert(match);
        var listType = match.match(/(^|\s)#/) ? 'ol' : 'ul';
        match = match.replace(/(^|\s)[\*#][ ]{0,1}/g, "$1");
        match = parseLists2(match);
        return '<'
                + listType + '><li>'
                + match.replace(/^\s/, '')
                .split(/\n/).join('</li><li>')
                + '</li></' + listType
                + '>';
    });
}
Run Code Online (Sandbox Code Playgroud)

我面临的唯一问题是如果我有如下模式:

* some ul item
* some ul item 
# some ol item
Run Code Online (Sandbox Code Playgroud)

ul项目不会被分隔为块,除非它被双线断开分开.

谢谢!

Wit*_*iko 1

您可以首先<li>使用这两个 ( 1 , 2 ) 正则表达式创建列表和项目符号的 s:

/\*\s*(([^\n]*(\n|$))*?)(?=\n|#|\*|<[uo]l>|$)/g;
 /#\s*(([^\n]*(\n|$))*?)(?=\n|#|\*|<[uo]l>|$)/g;
Run Code Online (Sandbox Code Playgroud)

然后,您可以使用另一个正则表达式连接相邻的<ul>s 和<ol>s :

/(<\/ul>\n?<ul>|<\/ol>\n?<ol>)/g;
Run Code Online (Sandbox Code Playgroud)

例子

以下代码片段演示了这一点:

/\*\s*(([^\n]*(\n|$))*?)(?=\n|#|\*|<[uo]l>|$)/g;
 /#\s*(([^\n]*(\n|$))*?)(?=\n|#|\*|<[uo]l>|$)/g;
Run Code Online (Sandbox Code Playgroud)
/(<\/ul>\n?<ul>|<\/ol>\n?<ol>)/g;
Run Code Online (Sandbox Code Playgroud)
txt1.onkeyup = txt1.onkeydown = txt1.onchange = replace;
replace();
  
function replace() {
  txt2.innerHTML = txt1.value.
    replace (/\*\s*(([^\n]*(\n|$))*?)(?=\n|#|\*|<[uo]l>|$)/g, "<ul><li>\n$1</li></ul>").
    replace ( /#\s*(([^\n]*(\n|$))*?)(?=\n|#|\*|<[uo]l>|$)/g, "<ol><li>\n$1</li></ol>").
    replace (/(<\/ul>\n?<ul>|<\/ol>\n?<ol>)/g, "");
}
Run Code Online (Sandbox Code Playgroud)