4

I guess this will be answered in two minutes but I am not able to google out a solution.

Tried: this, this

I have a textarea which first recieves some data from the server (dynamically using AJAX). The text in the textarea may look like this:

Hello&nbsp;Cruel&nbsp;<br&nbsp;/>World!

My users do not like the look of this :)

So I wrote a very simple function:

    function replaceHtml( string_to_replace ) 
    {
        var result = replaceAll( string_to_replace, "&nbsp;", " ");
        result = result.replace(/<br\s*\/?>/mg,"\n\r"); // or "\n", "\r", "\r\n"
        return result;
     }

My output looks like this:

Hello Cruel World!

Instead of:

Hello Cruel
World!

I would love a solution that is at most 5 lines long and can be applied with all browsers and OSes

Btw, Im no fan of regexes, so maybe the real problem will be there..

UPDATE

From this answer and mr Michael_B I got this solution, which is working for me, but I've got a hunch the character might not be the best solution there is:

function replaceHtml( string_to_replace ) 
{
    return string_to_replace.replace(/&nbsp;/g, ' ').replace(/<br.*?>/g, '\u2028');
}
Community
  • 1
  • 1
Igor L.
  • 3,159
  • 7
  • 40
  • 61

3 Answers3

8

Based on @Explosion Pills comment and jsFiddle

DEMO

function replaceHtml( string_to_replace ) 
{
        return string_to_replace.replace(/&nbsp;/g, ' ').replace(/<br.*?>/g, '\n');
}

UPDATE based on New line in text area

Updated DEMO

Maybe this will fix your issue with \n - Requires jQuery.

function replaceHtml(string_to_replace) {
    return $("<div>").append(string_to_replace.replace(/&nbsp;/g, ' ').replace(/<br.*?>/g, '&#13;&#10;')).text();
}
Community
  • 1
  • 1
Kaizen Programmer
  • 3,798
  • 1
  • 14
  • 30
  • I'm using Mozilla 19.0 and it's working fine for me. Are you looking at it on jsFiddle or in your own solution? – Kaizen Programmer Mar 05 '13 at 14:01
  • On jsFiddle it works, not on my solution.. I just tried to replace the '\n' part with this: '\u2028' from [this answer](http://stackoverflow.com/questions/6833269/javascript-platform-independent-line-separator) and it works on my solution, but I've got a hunch that's not the best way to do this.. Still I'll give you a +1 for a nice code, but till I figure out what's going on in the textarea, I don't know if it's ok to mark this question as answered. – Igor L. Mar 05 '13 at 14:11
  • Could you provide some more of your code...are you typing this info into the text area? What's calling `replaceHtml`? Can you recreate the problem you are having with `\n` in jsfiddle? – Kaizen Programmer Mar 05 '13 at 14:29
  • If I change my solution to `\u2028` it does not work on Chrome or Firefox on my machine. – Kaizen Programmer Mar 05 '13 at 14:34
  • There is a lot of code going on and I dont think I could recreate the problem in jsFiddle. I could write down the logic that's going on, and some source codes of what's happening, but it would be a lot of code and I do not think it will help you solve my problem.. By the way, the #13#10 thing didnt work on chrome as well.. – Igor L. Mar 08 '13 at 09:10
  • I understand. But if I can't recreate the problem, I can't really debug. Good luck! – Kaizen Programmer Mar 08 '13 at 13:37
1

Correct me if I'm wrong, but should it not be $.replaceAll() as it's a jQuery function not JS?

Or replace() in pure Javascript?

Matt Fletcher
  • 8,182
  • 8
  • 41
  • 60
0

You didn't need to use jQuery in there:

function replaceHtml( string_to_replace ) 
{
    var result = string_to_replace.replace(/\&nbsp;/g, ' ').replace(/<br\s*\/?>/mg,"\n\r"); // or "\n", "\r", "\r\n"
    return result;
 }
Belladonna
  • 3,824
  • 2
  • 24
  • 33