1

I'm using this search and replace jQuery script . I'm trying to put every character in a span but it doesn't work with unicode characters.

$("body").children().andSelf().contents().each(function(){
    if (this.nodeType == 3) {
        var $this = $(this);
        $this.replaceWith($this.text().replace(/(\w)/g, "<span>$&</span>"));
    }
});

Should I change the node type ? by what ?

thanks

4

2 回答 2

1

用“。”替换 \w (仅单词字符)(所有字符)

$("body").children().andSelf().contents().each(function(){
    if (this.nodeType == 3) {
        var $this = $(this);
        $this.replaceWith($this.text().replace(/(.)/g, "<span>$&</span>"));
    }
})
于 2013-03-19T14:01:56.990 回答
0

匹配“任何字符”的 RegEx 模式.不是\w(只匹配“单词字符”——在大多数 JS 中,字母数字字符和下划线[a-zA-Z0-9_])。注意.也匹配空格字符。要仅匹配和替换非空格字符,您可以使用\S.

有关 JS RegEx 语法的完整列表,请参阅文档

要替换任何和所有字符,请制作您的正则表达式/./g

$("body").children().andSelf().contents().each(function(){
    if (this.nodeType == 3) {
        var $this = $(this);
        $this.replaceWith($this.text().replace(/(.)/g, "<span>$&</span>"));
    }
});
于 2013-03-19T14:03:01.420 回答