einstein
einstein

Reputation: 13850

Remove line breaks in javascript

I haven't found a solution yet in StackOverlfow to my problem.

I'm using nl2br() in PHP to insert linebreaks <br> instead of \n when storing a message on the database. The thing is I want to have a preview of that message that has no linebreaks. HOw do I remove the linebreaks <br> ?

I have tried text.replace('<br>', '');

"Hi guys!"
<br>
<br>
"How are you?"

Should be

"Hi guys! guys how are you?"

Upvotes: 1

Views: 462

Answers (3)

Mike Samuel
Mike Samuel

Reputation: 120516

str.replace(/<br[^>]*>/gi, '')

Upvotes: 0

jojo
jojo

Reputation: 41

try this

 <script type="text/javascript">

    function ripBr(str,replacement){
        return str.replace(/<br*>/gi,replacement);
    }
 </script>

Upvotes: 0

Rudi Visser
Rudi Visser

Reputation: 21979

Could you post the exact code that you've tried and doesn't work?? This works fine:

<script type="text/javascript">
    var str = "Hi guys!<br><br>How are you?";
    str = str.replace(/<br>/g, " ");
    alert(str); // Gives "Hi guys!  How are you?" (Double spaced, due to 2 <br>'s being replaced)
    str = str.replace(/\s\s+/g, " ");
    alert(str); // Correct! "Hi guys! How are you?"
</script>

text.replace('<br>', ''); isn't a global replacement.

Upvotes: 1

Related Questions