Skip to content Skip to sidebar Skip to footer

Search And Replace Unicode Character

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(

Solution 1:

replace \w (only word caracters) by "." (all caracters)

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

Solution 2:

The RegEx pattern for matching "any character" is . not \w (that only matches 'word characters'—in most JS flavors the alphanumeric characters, and the underscore [a-zA-Z0-9_]). Note . also matches space characters. To only match and replace non-space characters, you can use \S.

For a full-listing of the JS RegEx syntax, see the documentation.

To replace any and all characters, make your regex /./g

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

Post a Comment for "Search And Replace Unicode Character"