Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/459.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/84.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript-为每个字母添加一个跨距_Javascript_Jquery_Regex - Fatal编程技术网

Javascript-为每个字母添加一个跨距

Javascript-为每个字母添加一个跨距,javascript,jquery,regex,Javascript,Jquery,Regex,我试图用许多“span”替换div的每个字母 此代码适用于带有“é”等重音的字母。你能帮我吗 $('h2').each(function(){ $(this).html($(this).text().replace(/(\w)/g, "<span>$&</span>")); }); $('h2')。每个(函数(){ $(this.html($(this.text().replace(/(\w)/g,“$&”); }); 您可以尝试使用以下正则表达式: /([

我试图用许多“span”替换div的每个字母

此代码适用于带有“é”等重音的字母。你能帮我吗

$('h2').each(function(){
  $(this).html($(this).text().replace(/(\w)/g, "<span>$&</span>"));
});
$('h2')。每个(函数(){
$(this.html($(this.text().replace(/(\w)/g,“$&”);
});

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

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

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

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

我的不带regexp的变体

var html=$('.test').html();
var ret=“”;
$.each(html.split(“”),函数(k,v){
ret+=“+v+”;
});
$('.test').html(ret);

就这些字母?没有数字、空格、!@$^&。。。,等等?原生JS正则表达式在Unicode字符方面存在问题。您是否只需要包装字母或任何字符?它与空格一起工作,但如果可能,最好使用数字和特殊字符。我不知道修改这个正则表达式。。。更重要的是人物!您可以通过使用
$('h2').text(function(){return$(this).text().replace(/(\w)/g,“$&”)}简化循环。
我知道这非常旧,但是如果我们还需要包含
&%/\()
这样的字符,该怎么办呢。但万一你需要在上面加个问号?
var html = $('.test').html();
var ret  = "";

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

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