带有 Unicode 属性 \\pL 或 \\p{L} 的 scalajs 正则表达式

scalajs regular expression with Unicode property \\pL or \\p{L}

需要任何语言的字母的正则表达式。 \pL 或 \p{L} 在 Ammonite Repl 1.6.8 (Scala 2.13.0 Java 11.0.8)

中工作
@ "\pL+".r.findFirstIn("abcßäöñ") 
res1: Option[String] = Some("abc\u00df\u00e4\u00f6\u00f1")

@ "\p{L}+".r.findFirstIn("abcßäöñ") 
res2: Option[String] = Some("abc\u00df\u00e4\u00f6\u00f1")

但是 Unicode 属性 在 Scalajs (1.5.1) 中不起作用,即结果 None。 任何帮助表示赞赏。 谢谢

好的,文档不支持 Unicode

您可以更改目标 ECMAScript 版本,在以下设置中进行更改:

scalaJSLinkerConfig ~= (_.withESFeatures(_.withESVersion(ESVersion.ES2018)))

此外,您可能需要在 build.sbt.

中添加 val ESVersion = org.scalajs.linker.interface.ESVersion

那你就可以使用

"(?U)\p{L}+".r.findFirstIn("abcßäöñ")
"(?U)[^\W\d_]+".r.findFirstIn("abcßäöñ")
"(?U)\p{Alpha}+".r.findFirstIn("abcßäöñ")

(?U)标志是UNICODE_CHARACTER_CLASS嵌入式标志选项,使\w\s\p{Alpha}等classes Unicode知道的。 [^\W\d_] 是一个 class 匹配除数字和下划线以外的任何字符,所以基本上是字母。 \p{Alpha}/\p{L} 将只匹配字母。

自 Scala.js 1.7.0 起,有条件地支持 Unicode 属性,例如 \p{L} 或其缩写形式 \pL。只有以下 sbt 设置才支持它们:

scalaJSLinkerConfig ~= (_.withESFeatures(_.withESVersion(ESVersion.ES2018)))

(如果使用其他构建工具,请参考该构建工具指定 Scala.js 链接选项的方式。)

启用该设置后,唯一不受支持的 \p{x} 属性是 x 格式为 In</code>代表一个Unicode<em>块</em>。 <code>L 不是 In... 的形式,所以它被支持。

参考:the Scala.js documentation on regex support:

Conditionally supported

[...]

The following features require esVersion >= ESVersion.ES2018 (which is false by default):

  • [...]
  • \p{} expressions where is not one of the POSIX character classes.