搜索并替换unicode字符
我正在使用此搜索并替换 jQuery脚本。 我试图将每个字符放在一个范围内,但它不适用于unicode字符。
$("body").children().andSelf().contents().each(function(){ if (this.nodeType == 3) { var $this = $(this); $this.replaceWith($this.text().replace(/(\w)/g, "$&")); } });
我应该更改节点类型吗? 通过什么方式 ?
谢谢
用“。”替换\ w(只有单词caracters)。 (所有人物)
$("body").children().andSelf().contents().each(function(){ if (this.nodeType == 3) { var $this = $(this); $this.replaceWith($this.text().replace(/(.)/g, "$&")); } })
用于匹配“任何字符”的RegEx模式是.
not \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, "$&")); } });