首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Regex: ONly允许大写和特定数字

Regex: ONly允许大写和特定数字
EN

Stack Overflow用户
提问于 2022-03-15 21:23:11
回答 1查看 167关注 0票数 0

我试图在javascript中创建regex并进行测试。

规则:文本应该只有8个字符。只包含大写和数字1-5,没有小写或特殊字符或6-7个数字.

示例:

代码语言:javascript
运行
复制
12345678 -- false
a2345678 -- false
aabbccdd -- false
AABB33DD -- true // contains BOTH uppercase and 
                    numbers between 1-5 and nothing else
AABB88DD -- false 
AABBCCDD -- false
AABB3.DD -- false 

代码:

代码语言:javascript
运行
复制
 var pattern = new RegExp("^(?=.*[1-5])(?=.*[A-Z]).+$");
 pattern.test(code)

我找不到合适的食客了。有人能帮忙吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-03-15 21:32:20

使用

代码语言:javascript
运行
复制
^(?=.*[1-5])(?=.*[A-Z])[A-Z1-5]{8}$

正则证明

解释

代码语言:javascript
运行
复制
--------------------------------------------------------------------------------
  ^                        the beginning of the string
--------------------------------------------------------------------------------
  (?=                      look ahead to see if there is:
--------------------------------------------------------------------------------
    .*                       any character except \n (0 or more times
                             (matching the most amount possible))
--------------------------------------------------------------------------------
    [1-5]                    any character of: '1' to '5'
--------------------------------------------------------------------------------
  )                        end of look-ahead
--------------------------------------------------------------------------------
  (?=                      look ahead to see if there is:
--------------------------------------------------------------------------------
    .*                       any character except \n (0 or more times
                             (matching the most amount possible))
--------------------------------------------------------------------------------
    [A-Z]                    any character of: 'A' to 'Z'
--------------------------------------------------------------------------------
  )                        end of look-ahead
--------------------------------------------------------------------------------
  [A-Z1-5]{8}              any character of: 'A' to 'Z', '1' to '5'
                           (8 times)
--------------------------------------------------------------------------------
  $                        before an optional \n, and the end of the
                           string
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71489198

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档