【问题标题】:JavaScript Regex to avoid certain pattern in textbox [closed]JavaScript正则表达式避免文本框中的某些模式[关闭]
【发布时间】:2016-11-16 06:27:57
【问题描述】:

我有一个验证要做。我的文本框中不应允许使用以下模式(post Man/PostMan/post Man 等)。
[P|p](OST|ost).*\s*[M|m][a|A][N|n]\s*(\d.)*

我必须向同一输入添加更多验证,例如: 允许:

  • 字母、数字和特殊字符“-”

而且,绝对不允许:

  • 任何其他特殊字符
  • 上面写的模式,我在文本框中输入的任何地方。

所以输入

  • 我在等邮递员
  • 我在等邮递员###
  • 邮递员有 不来,无效

而,

  • 我在等123348-12
  • 希望我的问题现在可以理解,有效

我尝试了以下方法:

([a-z 0-9 A-Z \\-])\*((?!\b[P|p]\*(OST|ost)\*\\.*\s\*[M|m][a|A][N|n]\s\*(\d.)\*\b))\*([a-z 0-9 A-Z])

但是一旦找到不匹配的字符,它就会停止工作。例如:

asdasd 666 # posttt -> 它停止验证特殊字符之后的任何内容。

我的正则表达式模式应该是什么?

【问题讨论】:

  • “它停止工作”是什么意思?你能告诉我们你使用正则表达式的代码吗?
  • 首先,忽略大小写:/p*(post)*\.*\s*man\s*(\d.)*/i。那么这就意味着我不能写I love that ost man!?
  • 这听起来对于单个正则表达式来说可能过于复杂。
  • 听起来你需要^(?!.*[P|p]*(OST|ost)*\.*\s*[M|m][a|A][N|n]\s*(\d.)*)[a-zA-Z-]*$,可以进一步修复和增强。
  • * 的意思不是“0 次或多次”吗?然后你的整个验证模式减少到[Mm][Aa][Nn](我怀疑|正在做你认为它正在做的事情)

标签: javascript regex regex-negation


【解决方案1】:

说明

^(?!.*?[Pp]*(OST|ost)*\.*\s*[Mm][Aa][Nn]\s*(\d.)*)[a-z0-9A-Z-]*$

** 要更好地查看图像,只需右键单击图像并选择在新窗口中查看

此正则表达式将执行以下操作:

  • 不允许您的第一个表达式匹配字符串中的任何位置
  • 然后将只允许a-zA-Z0-9-

我想指出,如果你的字符串只允许字母、数字和连字符,那么你的表达式中的点测试是不必要的。也可以使用不区分大小写标志来删除多个大小写字符集。

示例

现场演示

https://regex101.com/r/oY0hK2/1

示例文本

aWoeed1#fde39393aii
aWoeed1fde39393AII
aWoeed1fde39393AIIpostman 3a

示例匹配

aWoeed1fde39393AII

说明

NODE                     EXPLANATION
--------------------------------------------------------------------------------
  ^                        the beginning of the string
--------------------------------------------------------------------------------
  (?!                      look ahead to see if there is not:
--------------------------------------------------------------------------------
    .*?                      any character except \n (0 or more times
                             (matching the least amount possible))
--------------------------------------------------------------------------------
    [Pp]*                    any character of: 'P', 'p' (0 or more
                             times (matching the most amount
                             possible))
--------------------------------------------------------------------------------
    (                        group and capture to \1 (0 or more times
                             (matching the most amount possible)):
--------------------------------------------------------------------------------
      OST                      'OST'
--------------------------------------------------------------------------------
     |                        OR
--------------------------------------------------------------------------------
      ost                      'ost'
--------------------------------------------------------------------------------
    )*                       end of \1 (NOTE: because you are using a
                             quantifier on this capture, only the
                             LAST repetition of the captured pattern
                             will be stored in \1)
--------------------------------------------------------------------------------
    \.*                      '.' (0 or more times (matching the most
                             amount possible))
--------------------------------------------------------------------------------
    \s*                      whitespace (\n, \r, \t, \f, and " ") (0
                             or more times (matching the most amount
                             possible))
--------------------------------------------------------------------------------
    [Mm]                     any character of: 'M', 'm'
--------------------------------------------------------------------------------
    [Aa]                     any character of: 'A', 'a'
--------------------------------------------------------------------------------
    [Nn]                     any character of: 'N', 'n'
--------------------------------------------------------------------------------
    \s*                      whitespace (\n, \r, \t, \f, and " ") (0
                             or more times (matching the most amount
                             possible))
--------------------------------------------------------------------------------
    (                        group and capture to \2 (0 or more times
                             (matching the most amount possible)):
--------------------------------------------------------------------------------
      \d                       digits (0-9)
--------------------------------------------------------------------------------
      .                        any character except \n
--------------------------------------------------------------------------------
    )*                       end of \2 (NOTE: because you are using a
                             quantifier on this capture, only the
                             LAST repetition of the captured pattern
                             will be stored in \2)
--------------------------------------------------------------------------------
  )                        end of look-ahead
--------------------------------------------------------------------------------
  [a-z0-9A-Z-]*            any character of: 'a' to 'z', '0' to '9',
                           'A' to 'Z', '-' (0 or more times (matching
                           the most amount possible))
--------------------------------------------------------------------------------
  $                        before an optional \n, and the end of the
                           string

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-05-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-14
    • 1970-01-01
    • 2016-09-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多