views:

154

answers:

5

I have follow html code:

<a href="#">Evente (0)</a>

an i need:

<a href="#">Evente <span>0</span></a>

how i can change it with jQuery?

+3  A: 

I think you should use standard JS regexp:

str.replace(/\(/g, '<span>').replace(/\)/, '</span>')

SHiNKiROU
You should better use one regex for all of it to avoid having a single unmatched `</span>` if there is stuff like a smiley.And you might want to restrict the stuff inside the parentheses to what can occur in your app (instead of just `.*) to avoid false matches.
Marian
Thank You now it's working fine :)$("#primary-links a").text().replace(/\(/g, '<span>').replace(/\)/g, '</span>');
Vasta
A: 

Don't use jQuery.

myAtag.innerHTML = myAtag.innerHTML.replace(/\(/,'<span>').replace(/\)/,'</span>');

Better still, output it from the server like that in the first place.

Delan Azabani
I think that link is more appropriate for questions where there *could* be a jQuery alternative. To my knowledge, there is no bizarre jQuery substitute for JS regexes (at least, not yet).
karim79
A: 

My thought for using jquery;

I added an ID to the achor tag to make it simpler to reference:

<a id='target' href="#">Evente (0)</a>

And the following JQuery code

$('#target').html($('#target').html().replace("(","<span>").replace(")","</span"));

Similar to both of the above answers, but with JQuery

goggin13
A: 

Thank you SHiNKiROU now is running goooood ;)

$("#content-tabs a").text().replace(/\(/g, '<span>').replace(/\)/g, '</span>');
Vasta
A: 

Probably following is cleaner in regards of jQuery functionallity:

$('a').html(function(i,html){ return html.replace(/\((.*?)\)/, "<span>$1</span>")})
azatoth
This will be delete all a-tags!
Vasta