首页 > 解决方案 > 正则表达式不连贯的语法

问题描述

我正在尝试做一个正则表达式:

 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]);
}

当版本为:

但是什么时候:

我的问题在哪里?

谢谢

标签: javascripttypescript

解决方案


正如评论中已经指出的那样,您的重复应该在捕获组中。

请试试:

 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]);
}

推荐阅读