代码之家  ›  专栏  ›  技术社区  ›  SeaBass

是否从javascript中的字符串中筛选版本号?

  •  0
  • SeaBass  · 技术社区  · 6 年前

    我在这里找到了一些关于从字符串中提取版本号的线程,但没有一个线程能完全满足我的需要。

    如何使用javascript/regex从字符串中筛选出以下版本号?

    Title_v1_1.00.mov 过滤器 1

    v.1.0.1-Title.mp3 过滤器 1.0.1

    Title V.3.4A. 过滤器 3.4A

    V3.0.4b mix v2 过滤器 3.0.4b

    因此,寻找第一个出现的“v”或“v”,后面跟着一个数字,后面跟着数字、字母或点,直到字符串的结尾,或者直到出现一个空格,或者直到出现一个点(.)之后没有数字。

    3 回复  |  直到 6 年前
        1
  •  1
  •   The fourth bird    6 年前

    根据注释,要匹配字符串中的第一个版本号,可以使用捕获组:

    ^.*?v\.?(\d+(?:\.\d+[a-z]?)*)

    Regex demo

    将匹配:

    • ^ 断言字符串的开头
    • .*? 匹配0+任意字符非贪婪
    • v\.? 匹配v后接可选点
    • ( 捕获组
      • \d+ 匹配1 +数字
      • (?: 非捕获组
        • \.\d+[a-z]? 匹配一个点,1+数字后接可选字符a-z
      • )* 关闭非捕获组并重复0+次
    • ) 关闭捕获组

    如果角色喜欢 A 在V.3.4A中,只能在最后一部分中使用:

    ^.*?v\.?(\d+(?:\.\d+)*[a-z]?)

    const strings = [
      "Title_v1_1.00.mov filters 1",
      "v.1.0.1-Title.mp3 filters 1.0.1",
      "Title V.3.4A. filters 3.4A",
      "V3.0.4b mix v2 filters 3.0.4b"
    ];
    
    let pattern = /^.*?v\.?(\d+(?:\.\d+[a-z]?)*)/i;
    strings.forEach((s) => {
      console.log(s.match(pattern)[1]);
    });
        2
  •  0
  •   vrintle Jake    6 年前

    细节 :

  • v 字符“V”
  • (?:\.)? -匹配1或0个重复的“”。

  • 版本捕获组
  • [0-9a-z\.]* -匹配字母数字和“.”字符
  • [0-9a-z] -确保版本号不以“”结尾。
  • 你可以使用 RegExp.exec() 从字符串中提取匹配项的方法 逐一地 .

    const regex = /v(?:\.?)([0-9a-z\.]*[0-9a-z]).*/gi;
    
    let str = [
      "Title_v1_1.00.mov filters 1",
      "v.1.0.1-Title.mp3 filters 1.0.1",
      "Title V.3.4A. filters 3.4A",
      "V3.0.4b mix v2 filters 3.0.4b"
    ];
    
    let versions = [];
    let v; // variable to store match
    
    for(let i = 0; i < str.length; i++) {
      // Executes a check on str[i] to get the result of first capturing group i.e., our version number
      if( (v = regex.exec(str[i])) !== null) 
        versions.push(v[1]); // appends the version number to the array
      
      // If not found, then it checks again if there is a match present or not
      else if(str[i].match(regex) !== null) 
        i--; // if match found then it loops over the same string again
    }
    
    console.log(versions);
        3
  •  0
  •   Toto    6 年前

    var test = [
      "Title_v1_1.00.mov filters 1",
      "v.1.0.1-Title.mp3 filters 1.0.1",
      "Title V.3.4A. filters 3.4A",
      "V3.0.4b mix v2 filters 3.0.4b",
    ];
    console.log(test.map(function (a) {
      return a.match(/v\.?([0-9a-z]+(?:\.[0-9a-z]+)*)/i)[1];
    }));

    说明:

    /                       # regex delimiter
        v                   # letter v
        \.?                 # optional dot
        (                   # start group 1, it will contain the version number
            [0-9a-z]+       # 1 or more alphanumeric
            (?:             # start non capture group
                \.          # a dot
                [0-9a-z]+   # 1 or more alphanumeric
            )*              # end group, may appear 0 or more times
        )                   # end group 1
    /i                      # regex delimiter and flag case insensitive