2

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 ?

6 Answers 6

7

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.

Sign up to request clarification or add additional context in comments.

Comments

0

Using a double backslash \\n should help.

1 Comment

and a RegExp Object would, too. 'abc\ndef'.replace(new RegExp('\\n','g'),'\n>> ')
0

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

Comments

0

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

Comments

0

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, '>>'));

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.