Javascript - add a range for each letter

I am trying to replace each letter div with the set "span".

This code works except for letters with accents like "รฉ". Can you help me?

$('h2').each(function(){ $(this).html($(this).text().replace(/(\w)/g, "<span>$&</span>")); }); 
+4
source share
3 answers

You can try the following regular expression:

 /([^\x00-\x80]|\w)/g 
+7
source

\w does not contain diacritical characters, so you need to specify a Unicode range, for example

 /[az\u00C0-\u00F6\u00F8-\017E]/gi 
+5
source

My option without regex

http://jsfiddle.net/d6pDG/

 var html = $('.test').html(); var ret = ""; $.each(html.split(''), function(k, v) { ret += "<span>" + v + "</span>"; }); $('.test').html(ret); 
+3
source

Source: https://habr.com/ru/post/1489638/


All Articles