Collapsible bullets containing new lines from plain text

I am trying to parse a text document containing multiple bullets.

I would like to parse a marker with single newline characters, but would like to break when 2 or more newline characters are found.

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 ----------------------------------- 

This is what I have so far, I wrote a javascript function that can recursively parse mediawiki'sh ordered / unordered lists for html. The only difference is that the blocks are included in 2 line breaks versus the media window in 1 line break.

 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 + '>'; }); } 

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

I think the problem is the first regular expression - / (?: (?: (?: ^ | \ N) [* #]. *) +) / G to match bullts, this regular expression really breaks when a new line character, How can I capture marked lines with the character "1" of a new line?

I would like to analyze bullets with new lines in them and I would like to break a bullet only if there are 2 or more new string characters. and then the contents of the bullet.

[Edit] - I managed to make some changes, and the current version of my function looks below

 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 + '>'; }); } 

The only problem I am facing is if I have a template as shown below:

 * some ul item * some ul item # some ol item 

the ul element is not split as a block unless it is split by a double line break.

Thanks!

+6
source share
1 answer

You can first create lists and <li> for your bullets using these two ( 1 , 2 ) regexs:

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

Then you can join the adjacent <ul> and <ol> with another regular expression :

 /(<\/ul>\n?<ul>|<\/ol>\n?<ol>)/g; 

Example

The following snippet demonstrates this:

 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, ""); } 
 #txt1, #txt2 { width: 40%; height: 150px; display: inline-block; overflow-y: scroll; } 
 <textarea id="txt1"> * aaaa * bbbb # cccc # dddd This text is separate. </textarea><div id="txt2"></div> 
+1
source

Source: https://habr.com/ru/post/979594/


All Articles