Regex不能在引号前引起空格

问题描述 投票:0回答:1

我有一个用于捕获非法字符的正则表达式(针对特定的API,看起来像:

const regexIllegalChar: RegExp = new RegExp(
  `[^(\\w)(\\s)(\\')(\\.)(\\-)(\\/)(\\+)(\\=)(\\@)(\\_)(\\:)(\\,)(É)(È)(Ê)(Ë)(À)(Â)(Ä)(Ô)(Ö)(Û)(Ü)(Î)(Ï)]*`,
  'gi'
);

但是当我在IOS平板电脑上键入(看起来像<<时,会在空格的前面或后面自动添加一个空格(取决于打开或关闭的单词),并且不会捕获到该空格。

示例:

foo bar => foobar : OK
foo"bar (manualy removed automatic space) => foo"bar : OK
foo "bar (manualy removed automatic space, and add a normal one) => foo"bar : OK
foo[automatic_space]"bar => foo[automatic_space]"bar : NOK
javascript regex typescript
1个回答
0
投票

您可以尝试将合法字符列入白名单,而不是将非法字符列入黑名单,这将捕获IOS在文本中插入的未知字符。

(str.match(/[a-z0-9_"]+/gi) || []).join('')
© www.soinside.com 2019 - 2024. All rights reserved.