henryaaron
henryaaron

Reputation: 6192

Replacing right-angle brackets

My HTML looks something like this:

<b><a href="#">Home</a> > <a href="#">This Page</a></b>

How can I replace all instances of the plain text right-angle brackets > with these in JavaScript/jQuery?

The result should look like:

<b><a href="#">Home</a> ⟩ <a href="#">This Page</a></b>

I tried:

$('b').text($(this).text().replace('>','⟩'));

But it almost made my browser crash...

Thanks in advance.

Upvotes: 0

Views: 1122

Answers (2)

codingrose
codingrose

Reputation: 15709

Try:

var t = $("b").html();
$("b").html(t.replace(/&gt;/g,"}"));

Fiddle here.

Upvotes: 0

charlietfl
charlietfl

Reputation: 171689

Will have to isolate text nodes

In example <b> tag:

var content=$('b').contents();
content.each(function(){
    if( this.nodeType===3){/* text node*/       
        this.textContent=this.textContent.replace(/>/g,'}')        
    }
});

DEMO

Upvotes: 2

Related Questions