I'm trying to replace each letter of a div with many "span".
This code works except for letters with accents like "é". Can you help me please?
$('h2').each(function(){
$(this).html($(this).text().replace(/(\w)/g, "<span>$&</span>"));
});
I'm trying to replace each letter of a div with many "span".
This code works except for letters with accents like "é". Can you help me please?
$('h2').each(function(){
$(this).html($(this).text().replace(/(\w)/g, "<span>$&</span>"));
});
You can try with following regex:
/([^\x00-\x80]|\w)/g
\w
doesn't include diacritic symbols so you need to specify an unicode range, like this
/[a-z\u00C0-\u00F6\u00F8-\017E]/gi
My variant without regexp
var html = $('.test').html();
var ret = "";
$.each(html.split(''), function(k, v) {
ret += "<span>" + v + "</span>";
});
$('.test').html(ret);