正则表达式,包含13个字符的字母数字字符串,正好包含3个字符和10个数字

时间:2014-04-15 11:33:56

标签: javascript regex

我需要为13个字符的字母数字字符串创建一个正则表达式,其中包含3个字符和10个数字。

我正在尝试这个:

^(?=.*\d{10})(?=.*[a-zA-Z]{3})[0-9a-zA-Z]{13}$

但它不起作用。

3 个答案:

答案 0 :(得分:5)

如果订单无关紧要,我相信这应该有效:

/^(?=.{13}$)(\d*([A-Z]\d*){3})$/ig 要么 /^(?=.{13}$)([0-9]*([a-zA-Z][0-9]*){3})$/g

这分解如下:

  • ^:字符串开头
  • (?=.{13}$):向前看表达式 - 在继续执行实际的RegEx之前,先查找一些内容(在本例中为13个字符,然后是字符串的结尾)
  • \d*:找到0-9的任意数字(\ d等于[0-9])
  • ([A-Z]\d*){3}:找到一个A-Z然后任意数字0-9(x3找到你的三个alpha)
  • $:字符串结尾
  • i:忽略大小写
  • g:查找全局

答案 1 :(得分:1)

尝试此操作(不区分大小写,^ $匹配换行选项集

(?=^[a-z0-9]{13}$)([a-z]*[0-9][a-z]*){10}

(?=^[a-z0-9]{13}$)([^0-9]*[0-9][^0-9]*){10}

确保字符串中只有13个字符,并且只包含字母或数字

查找字符串

中的10个数字

编辑是从三位数变为十位数。

Regex Explanation

(?=^[a-z0-9]{13}$)(?:[^0-9]*[0-9][^0-9]*){10}

Options: Case insensitive; Exact spacing; Dot doesn't match line breaks; ^$ match at line breaks; Parentheses capture

确保行正好是13个字符,只包含字母和数字

Assert that the regex below can be matched, starting at this position (positive lookahead) «(?=^[a-z0-9]{13}$)»
   Assert position at the beginning of a line (at beginning of the string or after a line break character) (line feed) «^»
   Match a single character present in the list below «[a-z0-9]{13}»
      Exactly 13 times «{13}»
      A character in the range between “a” and “z” (case insensitive) «a-z»
      A character in the range between “0” and “9” «0-9»
   Assert position at the end of a line (at the end of the string or before a line break character) (line feed) «$»

确保行中只有十位数

Match the regular expression below «(?:[^0-9]*[0-9][^0-9]*){10}»
   Exactly 10 times «{10}»
   Match any single character NOT in the range between “0” and “9” «[^0-9]*»
      Between zero and unlimited times, as many times as possible, giving back as needed (greedy) «*»
   Match a single character in the range between “0” and “9” «[0-9]»
   Match any single character NOT in the range between “0” and “9” «[^0-9]*»
      Between zero and unlimited times, as many times as possible, giving back as needed (greedy) «*»

Created with RegexBuddy

答案 2 :(得分:0)

这是不优雅的,但它适用于我的所有测试:

/^(?=[\da-zA-Z]{13}$)(?=([^a-zA-Z]*[a-zA-Z]){3})(?!([^a-zA-Z]*[a-zA-Z]){4})(?=(\D*\d){10})(?!(\D*\d){11}).*$/

只要问一下它是否适合你,你想要一个解释,或者请提供一个不能正常工作的测试用例