最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

jquery - remove all instances of a character in a string with something else javascript - Stack Overflow

programmeradmin1浏览0评论

I need to replace all <br /> with a space. The below is the string that gets spitted out dynamically and i need to hide the br tags...

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

What am i doing wrong?is it possible to replace all other br tags with a ma except the last one, which will be repalced by a space

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

I need to replace all <br /> with a space. The below is the string that gets spitted out dynamically and i need to hide the br tags...

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

What am i doing wrong?is it possible to replace all other br tags with a ma except the last one, which will be repalced by a space

$('.WorkingHours').text().replace(/<br />/g, " "); 
Share Improve this question edited Mar 14, 2012 at 14:57 Anjana Sharma asked Mar 14, 2012 at 14:55 Anjana SharmaAnjana Sharma 4,7655 gold badges40 silver badges51 bronze badges 1
  • stackoverflow./questions/2145988/jquery-string-replace Answer is here. – PhyBandit Commented Mar 14, 2012 at 14:58
Add a ment  | 

5 Answers 5

Reset to default 5

There are three issues with your code:

  1. $('.WorkingHours').text() won't contain any HTML (so no br tags either), it only returns the text content of elements.
    $('.WorkingHours').text() returns:

    M,W,Th,F 7:30 AM - 4:00 PMTu 7:30 AM - 6:00 PM

    whereas $('.WorkingHours').html() returns:

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

  2. You have to escape the inner backslash in your expression. Edit: Having a look at the output of .html() it actually does not contain <br /> but <br>. This might depend on the doctype of the document (not working example, working example).

  3. You have to assign the value back to the element.

You might be able to do

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

but it would be much cleaner to not use regular expressions at all:

$('.WorkingHours').find('br').replaceWith(' ');

This finds all br element nodes and replaces them with a text node containing only a space.

DEMO

Update (in response to one of your ments): If you want to replace the last br with a full stop, you can use .last():

$('.WorkingHours')
 .find('br').last().replaceWith('.')
 .end().replaceWith(' ');

DEMO

Couple things:

use html() to get the HTML of the node:

var rawHTML = $('.WorkingHours').html();

Use \ to escape the / within your regex:

/<br \/>/g

Set the HTML to the return value of the replace function:

$('.WorkingHours').html(rawHTML.replace(/<br \/>/g, ' ');

End Product:

var rawHTML = $('.WorkingHours').html();
$('.WorkingHours').html(rawHTML.replace(/<br \/>/g, ' ');

using .text() instead of .html() to remove html tags

.text() only returns the inner text, instead of the inner html.

I think you need to escape the forward-slash in the expression, e.g.

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

You need to escape the forward slash in the <br />:

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

http://jsfiddle/uLYrH/1

发布评论

评论列表(0)

  1. 暂无评论