Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript - add a span for each letter

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>"));
});
like image 734
jlafforgue Avatar asked Dec 06 '22 06:12

jlafforgue


2 Answers

You can try with following regex:

/([^\x00-\x80]|\w)/g
like image 128
hsz Avatar answered Dec 22 '22 09:12

hsz


\w doesn't include diacritic symbols so you need to specify an unicode range, like this

/[a-z\u00C0-\u00F6\u00F8-\017E]/gi
like image 20
Fabrizio Calderan Avatar answered Dec 22 '22 09:12

Fabrizio Calderan