【问题标题】:Joi - Invalid Regular ExpressionJoi - 无效的正则表达式
【发布时间】:2018-10-21 18:09:06
【问题描述】:
/^(?! )(?=.*[a-z])(?=.*[A-Z])(?=.*\d)([a-zA-Z\d!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~ ]{8,})(?<! )$/

在 regex101.com、chrome 控制台甚至 joi npm runkit 中完美运行。但是当在代码中使用时,Joi 会给出错误 -

SyntaxError: Invalid regular expression: {above regex}: Invalid Group

你能帮我解决这个问题吗?

【问题讨论】:

  • (?&lt;! ) 是一个lookbehind,目前它只适用于Chrome(支持ECMAScript 2018)。
  • 我在 joi npm runkit 中尝试过。它工作得很好,但是在代码中使用时它给了我错误
  • 使用this fix
  • 通过更新我的 npm 库解决了这个问题
  • thanx @WiktorStribiżew 你的解决方案有帮助

标签: node.js regex joi


【解决方案1】:

请注意,不支持 ECMAScript 2018 的浏览器不支持正则表达式中的lookbehinds。

您拥有的模式包含 (?&lt;! ) 否定后向查找,用于检查字符串末尾是否没有空格(它位于 $ 锚点之前)。

因此,您可以通过用(?=.*\S$)(字符串末尾需要一个非空白字符)或(?!.*\s$)(字符串末尾不允许有空格)替换该lookbehind 来修复它lookaheads 受流行的 ES5 标准支持。

【讨论】:

    猜你喜欢
    • 2019-01-06
    • 1970-01-01
    • 2020-08-11
    • 2011-08-09
    • 2019-08-03
    • 1970-01-01
    • 1970-01-01
    • 2015-07-08
    • 1970-01-01
    相关资源
    最近更新 更多