javascript - Regex for converting indented lines into code blocks -


i making regex parses code blocks, problem is, there problem!

now, code blocks so's, 4+ spaces or 1+ tab. problem is, regex grabbing first , last lines of code blocks :( have no clue why.

simply, regex:

/^(?:( {4,}|\t+)(.+))$(?:(?:(?:\n?)( {4,}|\t+)(.+))*)$/ 

then using this:

    .replace(/^(?:( {4,}|\t+)(.+))$(?:(?:(?:\n?)( {4,}|\t+)(.+))*)$/gmi, function (m, g1, g2, g3, g4) {         return "<code>" + (g1 + g2) + ("\n" + g3 + g4) + "</code>";     }) 

so question is, how can return of lines, , not first\last lines?

bonus there way can remove 4 spaces/1 tab before displaying in code block? :d


edit

i show example of not work:

works:

    line #1, 4 spaces     line #2, 4 spaces 

does not work:

    line #1, 4 spaces     line #2, 4 spaces     line #3, 4 spaces 

what display lines #1 , #3.


edit 2

just in-case want full context of code, here is:

    .replace(/^(?:( {4,}|\t+)(.+))$(?:(?:(?:\n?)( {4,}|\t+)(.+))*)$/gmi, function (m, g1, g2, g3, g4) {         var marker = '(-{{ +_~ ' + codeblocks.length + ' ~_+ }}-)';         !g3 ? codeblocks.push(g1 + g2) : codeblocks.push((g1 + g2) + ("\n" + g3 + g4));         return marker;     }) //block     //other styling don't want code blocks affected by, that's why hide them first     .replace(/\(-\{\{ \+_~ ([0-9]*) ~_\+ \}\}-\)/g, function (m, g1) {         return codeblocks[parseint(g1, 10)] ? "<code>" + codeblocks[parseint(g1, 10)] + "</code>" : m;     }) //block 

i'm using jquery , text areas demonstrate proof of concept, may want adjust use of newline chars used, here's take:

<textarea id='code' cols='50' rows='10'></textarea> <textarea id='codeblock' cols='50' rows='10'></textarea> <script> $('#code').on('keyup',function() {   var code=$(this).val().replace(/(( {4}|\t)(.*?(\n|$)))+/g, function (m) {     return "<code>" + m.replace(/(^|\n)( {4}|\t)/g,'$1') + "</code>";   });   $('#codeblock').val(code); }); </script> 

Comments

Popular posts from this blog

java - UnknownEntityTypeException: Unable to locate persister (Hibernate 5.0) -

python - ValueError: empty vocabulary; perhaps the documents only contain stop words -

ubuntu - collect2: fatal error: ld terminated with signal 9 [Killed] -