Search code examples
javascriptjqueryreplacespecial-characters

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.


Solution

  • 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