检测 Zalgo 的正则表达式

Regex to Detect Zalgo

我正在创建一个消息过滤系统,用于检测 z͎͗ͣḁ̵̑l̉̃ͦg̐̓̒o͓̔ͥ。

我当前的正则表达式是 /([^\u0009-\u02b7\u2000-\u20bf\u2122\u0308]|(?![^aeiouy])\u0308)/gm 但这也捕获了表情符号。

正则应过滤所有w̵̢̃ë̸̩́ị̵̽r̴̺̆d̴̘̕e̷̳̅x̷̮́ṱ̸̏x̷̮́ṱ̸̏i̶̧͐k̸̗̈ě̸͖k̸̗̈ě̸͖t̶̛̤h̸̰̔i̵̿͜s̴̛或t̶e̶x̴t̸iph̵i̶i̶k̷e̸k̷e̸k̷e̸t̷h̵i̷s̴s̴,但不应该捕获表情符号。

测试方法如下

const re = /%CC%/g
const hasZalgo = txt => re.test(encodeURIComponent(txt));  

console.log(hasZalgo("w̵̢̃2ë̸̩́ị̵̽r̴̺̆d̴̘̕ ̴͎́ẗ̷͕́e̷̳̅x̷̮́ṱ̸̏ ̸̜͒ḻ̵̎i̶̧͐k̸̗̈ě̸͖ ̸̥̄t̶̛̤h̸̰̔i̵̿͜ş̴̛ 222 "))
console.log(hasZalgo("Weird text like %CC% this "))

转换方法如下

console.log(
  decodeURIComponent(
    encodeURIComponent("w̵̢̃2ë̸̩́ị̵̽r̴̺̆d̴̘̕ ̴͎́ẗ̷͕́e̷̳̅x̷̮́ṱ̸̏ ̸̜͒ḻ̵̎i̶̧͐k̸̗̈ě̸͖ ̸̥̄t̶̛̤h̸̰̔i̵̿͜ş̴̛ 222 ")
    .replace(/%CC(%[A-Z0-9]{2})+%20/g," ") // replace space
    .replace(/%CC(%[A-Z0-9]{2})+(\w)/g,"") // replace anything else
  )
)