0

I am trying to filter Chinese characters from a string.

According to this answer, I can do:

const nonChinese = /[^\p{Script=Han}]/gimu;
const text = "asdP asfasf这些年asfagg 我开源的 几个小项目sad asd";
console.log(text.replace(nonChinese, ""));

Expected output:

"这些年我开源的几个小项目"

This works in an online compiler like js fiddle: js fiddle playground, but it does not work on my NextJs (with typscript) application and it gives me an compile error:

Error: Failed to recognize value `Han` for property `Script`.

This error is raised from:

index.js?46cb:365 Uncaught     at getUnicodePropertyValueSet (file://D:\code\personal-website\node_modules\next\dist\compiled\babel-packages\packages-bundle.js:315:46438)
    at getUnicodePropertyEscapeSet (file://D:\code\personal-website\node_modules\next\dist\compiled\babel-packages\packages-bundle.js:315:46885)
    at processCharacterClass (file://D:\code\personal-website\node_modules\next\dist\compiled\babel-packages\packages-bundle.js:315:47864)
    at processTerm (file://D:\code\personal-website\node_modules\next\dist\compiled\babel-packages\packages-bundle.js:315:48494)
    at rewritePattern (file://D:\code\personal-website\node_modules\next\dist\compiled\babel-packages\packages-bundle.js:315:50577)
    at PluginPass.RegExpLiteral (file://D:\code\personal-website\node_modules\next\dist\compiled\babel-packages\packages-bundle.js:74:17966)
    at i (file://D:\code\personal-website\node_modules\next\dist\compiled\babel\bundle.js:1890:373015)
    at NodePath._call (file://D:\code\personal-website\node_modules\next\dist\compiled\babel\bundle.js:1890:292626)
    at NodePath.call (file://D:\code\personal-website\node_modules\next\dist\compiled\babel\bundle.js:1890:292450)
    at NodePath.visit (file://D:\code\personal-website\node_modules\next\dist\compiled\babel\bundle.js:1890:293401)
    at processResult (file://D:\code\personal-website\node_modules\next\dist\compiled\webpack\bundle5.js:49593:19)
    at <unknown> (file://D:\code\personal-website\node_modules\next\dist\compiled\webpack\bundle5.js:49695:5)
    at <unknown> (file://D:\code\personal-website\node_modules\next\dist\compiled\webpack\bundle5.js:140993:11)
    at <unknown> (file://D:\code\personal-website\node_modules\next\dist\compiled\webpack\bundle5.js:140845:18)
    at context.callback (file://D:\code\personal-website\node_modules\next\dist\compiled\webpack\bundle5.js:140718:13)
    at <unknown> (file://D:\code\personal-website\node_modules\next\dist\build\babel\loader\index.js:33:61)

May I know why this happens and how to fix it?

Kallzvx
  • 594
  • 7
  • 23

1 Answers1

0

Well, not sure why but after I changed the regex to:

const nonChinese = /[\P{Script=Han}]/gim

It just worked, if anyone is still facing the problem and you have only ASCII and Chinese characters, you can use an alternative that filters out all ASCII characters instead.

const allAscii = /[\x00-\x7F]/gim
Kallzvx
  • 594
  • 7
  • 23