0

I like the way stackexchange does quotes, I attempted to add it to my site, but didn't get it to work across multiple lines, only one line. What do I need to change to do multiple lines?

Here is what I am currently using:

val = val.replace(/^>(.+?)($|\n)/ig, "<blockquote>$1</blockquote>");


Edit
I decided to go for a different style.

val = val.replace(/""([\s\S]*.+?)""/igm, "<blockquote>$1</blockquote>");
Get Off My Lawn
  • 34,175
  • 38
  • 176
  • 338

2 Answers2

0

I abandoned doing this in regex.

var a = '>abc\n>def\n>ghi'.split( /\n/ ); // [">abc", ">def", ">ghi"]

var previousInBlock = false;
var currentInBlock = false;
var nextInBlock = false;

for( var i = 0; i < a.length; i++ ) {

    previousInBlock = currentInBlock;
    currentInBlock = a[i].indexOf( ">" ) === 0;
    nextInBlock = ( i + 1 ) < a.length ? a[i+1].indexOf( ">" ) === 0 : false;

    if ( currentInBlock ) {
        a[i] = a[i].slice( 1 );

        if( !previousInBlock ) { 
            a[i] = "<blockquote>" + a[i]; 
        }

        if ( !nextInBlock ) { 
            a[i] = a[i] + "</blockquote>"; 
        }
    }
}

a.join( "\n" );

The above code can handle multiple sets of blockquotes.

Bruno
  • 5,772
  • 1
  • 26
  • 43
0

Instead of replacing, try to match against each line, then combine the matched lines into one string.

Something like this:

var val = '>abc\n>def\n>ghi',
    regex = /(?:^>)(.+)(?:$|\n)/img,
    match = '', quote = [];

while((match = regex.exec(val)) !== null){
  quote.push(match[1]);
}

val = '<blockquote>'+quote.join('<br/>')+'</blockquote>';

DEMO: http://jsfiddle.net/k7DVu/

gen_Eric
  • 223,194
  • 41
  • 299
  • 337
  • @RyanNaddy: Oh, you mean if there are multiple sets in the same string? EDIT: Yeah, I just tested this, and it will combine all quotes into one tag. – gen_Eric Jan 10 '13 at 23:48