简体   繁体   中英

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>"));
});

您可以尝试使用以下正则表达式:

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

\\w不包括变音符号,因此您需要指定unicode范围,如下所示

/[a-z\u00C0-\u00F6\u00F8-\017E]/gi

My variant without regexp

http://jsfiddle.net/d6pDG/

var html = $('.test').html();
var ret  = "";

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

$('.test').html(ret);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM