javascript - 正则表达式不连贯的语法
问题描述
我正在尝试做一个正则表达式:
static regex(version: string) {
const match = /^([0-9])*\.([0-9])*\.([0-9])*-([0-9])*-(toto)$/.exec(version);
return parseInt(match[1]) + "." + parseInt(match[2]) + "." + parseInt(match[3]) + "-" + parseInt(match[4]);
}
当版本为:
- 1.0.0-7-toto,它还给我 1.0.0-7 所以很好
但是什么时候:
- 1.0.0-17-toto,它返回我 1.0.0-7 而不是 1.0.0-17
我的问题在哪里?
谢谢
解决方案
正如评论中已经指出的那样,您的重复应该在捕获组中。
请试试:
static regex(version: string) {
const match = /^([0-9]*)\.([0-9]*)\.([0-9]*)-([0-9]*)-(toto)$/.exec(version);
return parseInt(match[1]) + "." + parseInt(match[2]) + "." + parseInt(match[3]) + "-" + parseInt(match[4]);
}
推荐阅读
- swift - 将 CharacterSet 转换为字符串或数组或字符集
- regex - Nginx,重定向的正则表达式无法正常工作
- java - Avro 模式演变在反序列化时抛出 EOFException
- android - 如何通过选择带有位置自动完成功能的标记来绘制路线?
- c# - 如何使用 c# 命令安装/添加虚拟打印机?
- javascript - 更改时引导日期选择器
- javascript - 在 ckeditor 中插入 html 会导致语法错误(laravel)
- filter - 如何过滤非空白行?
- jquery - 如何使用 jQuery 将动态数据添加到 selectpicker 上的选项?
- android - com.bumptech.glide.load.engine.GlideException 类:加载资源失败