0
<span class="WorkingHours">
   M,W,Th,F 7:30 AM - 4:00 PM &lt;br /&gt;Tu 7:30 AM - 6:00 PM &lt;br /&gt;
</span>

will be rendered as this

M,W,Th,F 7:30 AM - 4:00 PM <br />Tu 7:30 AM - 6:00 PM <br />

Now i need to replace the &lt;br /&gt; as an empty space before it is rendered?? I already asked a similar question, but i underestimated the trickiness of this.. I did this

$('.WorkingHours').text().replace(/<br \/>/g, " ");

didn't work..can someone help me out of this?

Shouldn't this work?

$('.WorkingHours').text().replace(/&lt;br \&gt;/g, " "); 
Anjana Sharma
  • 4,535
  • 5
  • 37
  • 51
  • You need to escape the < and > characters. try this `replace(/\
    /g, " ");`
    – Austin Brunkhorst Mar 14 '12 at 15:44
  • If you use `.text()` instead of `.html()` and the code that uses the regular expression [from my answer to your previous question](http://stackoverflow.com/questions/9704362/remove-all-instances-of-a-character-in-a-string-with-something-else-javascript), it will just work fine: http://jsfiddle.net/K9jVH/. Instead of creating a new question, you should have clarified your original one. That said, you should fix the HTML generation in the first place. – Felix Kling Mar 14 '12 at 15:46
  • @FelixKling Sorry about that! will do that in the future – Anjana Sharma Mar 14 '12 at 15:47
  • @FelixKling Thank you so much, it worked great! and sorry about the repost! Will keep that in mind! – Anjana Sharma Mar 14 '12 at 15:49

3 Answers3

2

This will work:

$('.WorkingHours').text(function(i, text) {
    return text.replace(/<br\s*\/?>/g, " "); 
});

DEMO

In this case .html() would return the HTML entities (&lt;) whereas .text() returns the decoded characters (i.e. <). Given that,

$('.WorkingHours').text().replace(/<br \/>/g, " ")

actually works as well but you still have to assign the result back to the element.

See also my previous answer.


Regarding replacing the last <br /> with a different character: It might be possible with regular expressions, but in that case, I would just parse the text as HTML and perform the same operation as I showed in my previous answer:

var $content = $('<div />').html($('.WorkingHours').text());
$content 
 .find('br').last().replaceWith('.')
 .end().replaceWith(' ');

$('.WorkingHours').html($content.html());

DEMO

Community
  • 1
  • 1
Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
0

you have to escape the HTML characters to complete the pattern...

replace(/\<br \/\>/g, " ");

Eric Hodonsky
  • 5,617
  • 4
  • 26
  • 36
0

If you have html entities in markup try replacing those instead

   replace('&lt;br \/&gt;', ' ')

Looks like you were missing a "/"

charlietfl
  • 170,828
  • 13
  • 121
  • 150