views:

100

answers:

6

Hey there, I'm trying to replace a

<blockquote>...</blockquote>

with

>> ...

This is my Code:

var testhtml = 'sdkjhfbs <blockquote>skldfjsfkjghbs\n sdjkfhb ksdbhv isl\n kdjbhdfgkj bs</blockquote>kdjfgnkdfj';
alert(blockquoteConvert(testhtml));

function blockquoteConvert(html) {
    return '>>' + html.
        replace(/<blockquote>([^]+)<\/blockquote>/gi,"$1").
        replace('/\n/','\n>> ');
}

But it doesn't find the Linebreaks. (I checked with indexOf('\n')).

How can I do this ?

A: 

Using a double backslash \\n should help.

Pekka
and a RegExp Object would, too. `'abc\ndef'.replace(new RegExp('\\n','g'),'\n>> ')`
ZJR
+7  A: 

Try it without the quotes:

replace(/\n/g,'\n>> ')

Now the delimiters are part of the literal regular expression declaration syntax and not part of the pattern itself.

Gumbo
A: 

You need to do a global replace, or otherwise the replace will match only the first newline. Also, you can't use quotes around your regular expression as the slashes will become part of the search string, so try this:

replace(/\n/g,'\n>> ')
Tatu Ulmanen
A: 

You were close, but you weren't consistent with the syntax:

function blockquoteConvert(html) {
    return '>> ' + html.
        replace(/<blockquote>([^]+)<\/blockquote>/gi,"$1").
        replace(/\n/g,'\n>> ');
}
Jimmy Cuadra
A: 

Try this

var testhtml = 'sdkjhfbs <blockquote>skldfjsfkjghbs\n sdjkfhb ksdbhv isl\n kdjbhdfgkj bs</blockquote>kdjfgnkdfj';
alert(blockquoteConvert(testhtml));

function blockquoteConvert(id) {
car text = document.getElementById(id).value;
text = text.replace(/\n\r?/g, '>>');
}


Or use jquery 
$('#caption').html($('#caption').text().replace(/\n\r?/g, '>>'));
streetparade
A: 

Okay, now I'm confused. Try this please:

var testhtml = 'sdkjhfbs <blockquote>skldfjsfkjghbs\n sdjkfhb ksdbhv isl\n kdjbhdfgkj bs</blockquote>kdjfgnkdfj';
alert(convertLineBreaks(testhtml));
alert(blockquoteConvert(testhtml));

function blockquoteConvert(html) {
    return html
        .replace(/<blockquote>([^]+)<\/blockquote>/gi,convertLineBreaks("$1"));
}

function convertLineBreaks(text) {
    return '>>' + text.replace(/\n/g,'\n>> ');
}

After the replacement of blockquote, my linebreaks seem to be lost... ?

janoliver