javascript - 在javascript中,我们可以使用正则表达式将字符串与循环部分匹配,但不要拆分它

标签 javascript regex

像这样的字符串:"01A123,02A13334,03A99313,01BA9424,……" 子字符串的正则表达式是:/\d{2}[A-Z]{1,2}\d*/

我们可以编写一个正则表达式来匹配该字符串而不拆分它吗?

最佳答案

要验证整行是否具有这种形式,如下所示

 #  /^\d{2}[A-Z]{1,2}\d*(?:,\d{2}[A-Z]{1,2}\d*)*$/

 ^                             # Beginning of string
 \d{2} [A-Z]{1,2} \d*          # 2 digits, 1-2 A-Z, optional 0-many digits
 (?:                           # Cluster group start (non-capture group)
      ,                             # comma ','
      \d{2} [A-Z]{1,2} \d*          # 2 digits, 1-2 A-Z, optional 0-many digits
 )*                            # Cluster group end, optional 0-many times
 $                             # End of string

关于javascript - 在javascript中,我们可以使用正则表达式将字符串与循环部分匹配,但不要拆分它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22613744/

相关文章:

javascript - 关于在 React 中使用 ES6 类

javascript - 将 asar 包中的打包 JS 文件传递​​给生成的 Node 子进程

javascript - 将 Canvas 中上传的图像保存到服务器

javascript - 在每个图像顶部动态显示图标

javascript - VScode 在匹配前后替换正则表达式

java - 如何从这个字符串中解析出时间戳?

python - 在读取文件期间用单个换行符替换多个换行符

Python 正则表达式匹配特殊字符

javascript - Express.js 中设置和获取 cookie 失败

regex - 使用正则表达式前瞻,egrep