Let’s say I have .contents() of HTML. How can I remove all dashes (secondary method is I need a .replace()) only within class=“” (e.g. class=“asdf-asdf” would turn out to class=“asdfasdf”).
I did search but I’m having trouble finding the correct search terms. There are examples of removing dashes via .replace() but I don’t know how to combine that to look only within the class attribute?
Hmm, thanks for that. I really appreciate it. It’s erroring out for me though
var twt = $(obj).find('iframe[id*="twitter-widget"]').contents();
var tweets = twt.find('.timeline-TweetList-tweet');
var regex = /class="(.*?)"/g;
var newtweets=tweets.replace(regex, function(className) {
return className.replace(/-/g, '_');
})
http://cod ere po.d em o.fin alsi te.com/bp t/mas hup2
This doesn’t change with it being in an iframe, does it?
Thanks! Now it’s not erroring out, and appears to be console logging the right stuff, but it’s not actually replacing.
var lastChild = $(obj).find(".tweets > li:last-child").html();
var regex = /class="(.*?)"/g;
lastChild=lastChild.replace(regex, function(className) {
console.log(className.replace(/-/g, '_'));
return className.replace(/-/g, '_');
});
I know .replace() doesn’t actually replace unless you overwrite the variable (a.k.a. why I did lastChild=lastChild) but it doesn’t seem to have overridden?
Yes, you’re only updating the html in the lastChild variable, not applying it back to the DOM.
var $el = $(obj).find(".tweets > li:last-child")
var html = $el.html();
var regex = /class="(.*?)"/g;
html = html.replace(regex, function(className) {
return className.replace(/-/g, '');
});
$el.html(html);
Your initial question led me down the string replacement path but jQuery has a way simpler way to achieve this. This version will only update the className’s that need updating, the previous will replace the whole elements innerHTML.
You won’t believe this, but I changed that to replace the dash with a dash, and now I have styling working in Safari. I KNEW those dashes were corrupt…thank you thank you!
Sounds like an encoding problem to me, to confirm you can try logging the charCode of the two dash characters, I suspect they’ll give you two different numbers.