1

私はこの検索を使用して、 jQueryスクリプトを置き換えています。すべての文字をスパンに入れようとしていますが、Unicode文字では機能しません。

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

ノードタイプを変更する必要がありますか?何によって ?

ありがとう

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

「任意の文字」に一致する正規表現パターンはそうではあり.ません\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 に答える