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');
}
7
  • I'm not sure, but try '\n\r' instead of "\n\r" (simple quote replace double quotes)
    – JoDev
    Mar 5, 2013 at 13:45
  • 3
    Seems to work: jsfiddle.net/Xfffg Mar 5, 2013 at 13:49
  • @joDev Single quotes and double quotes are equivalent in Javascript Mar 5, 2013 at 13:49
  • replaceAll isn't a function Mar 5, 2013 at 13:54
  • @Explosion Pills: can't believe it, but its not working for me.. function replaceHtml( string_to_replace ) { var result = string_to_replace.replace(/&nbsp;/g, ' ').replace(/<br.*?>/g, '\n'); return result; }
    – Igor L.
    Mar 5, 2013 at 14:04

3 Answers 3

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();
}
6
  • 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? Mar 5, 2013 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 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 5, 2013 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? Mar 5, 2013 at 14:29
  • If I change my solution to \u2028 it does not work on Chrome or Firefox on my machine. Mar 5, 2013 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 8, 2013 at 9:10
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?

1
  • Honestly: did not know there was a $.replaceAll() :)
    – Igor L.
    Mar 5, 2013 at 14:08
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;
 }

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.