delled delled - 1 year ago 67
jQuery Question

jquery - find the character in the string and wrap the remaining character

I have a heading like:


How do I search for the character '|', then wrap it in span along with the remaining character in the right?

The result should be:

foobar< span>|foo< /span>.

In the website we have different length headings like:

hello|my mario
you are the best|my friend

So i want to detect the '|' character and wrap it with the remaining.

Answer Source

inspired by @guedes answer

try this:

var headingText = $('h2').text();
var newText = ( headingText.replace(/\|[^]*$/, ' <span>$&</span>') );

<h2 class="heading">foobar|foo</h2>

you can have any heading you like.e.g



Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download