0

I found some threads about extracting version number from a string on here but none that does exactly what I want.

How can I filter out the following version numbers from a string with javascript/regex?

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

So look for the first occurrence of: "v" or "v." followed by a digit, followed by digits, letters or dots until either the end of the string or until a whitepace occurs or until a dot (.) occurs with no digit after it.

7
  • Try v\.?(\d+(?=\S)(?:\.\d+[a-z]?)*) see demo Commented Oct 21, 2018 at 8:27
  • @Thefourthbird Looking good. How do I make so it also takes the most simple version "v1"? Commented Oct 21, 2018 at 8:30
  • Do you mean in V3.0.4b mix v2 it should also match v2? Commented Oct 21, 2018 at 8:32
  • @Thefourthbird No, but a new example would be Title v1 V3.0.4b mix v2 where it should match just 1 for "v1". It seems like you need something after the first digit for it to match, which I realize I may have been misleading in the original post. Commented Oct 21, 2018 at 8:33
  • Then you could use an anchor ^. See this demo Should the [a-z] only be after the last digit like 3.4Aor could it also be 3.4A.5B Commented Oct 21, 2018 at 8:36

3 Answers 3

1

As per the comments, to match the first version number in the string you could use a capturing group:

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

Regex demo

That will match:

  • ^ Assert the start of the string
  • .*? Match 0+ any character non greedy
  • v\.? Match v followed by an optional dot
  • ( Capturing group
    • \d+ Match 1+ digits
    • (?: Non capturing group
      • \.\d+[a-z]? Match a dot, 1+ digits followed by an optional character a-z
    • )* Close non capturing group and repeat 0+ times
  • ) Close capturing group

If the character like A in V.3.4A can only be in the last part, you could use:

^.*?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]);
});

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you! My last wish. I need v1a to return 1a and v1ab.1abc to return 1ab.1abc. So a dot always need to be followed by a digit and then optional number of letters after that. Thanks again!!
Try this demo by adding a character class and repeat that 0+ times
Genius! Thanks so much :D
0

Details:

  • v - character "v"
  • (?:\.)? - matches 1 or 0 repetition of "."

  • Version capturing group
  • [0-9a-z\.]* - Matches alphanumeric and "." character
  • [0-9a-z] - ensures that version number don't ends with "."
  • You can use RegExp.exec() method to extract matches from string one by one.

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

    Comments

    0

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

    Explanation:

    /                       # 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
    

    Comments

    Your Answer

    By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

    Start asking to get answers

    Find the answer to your question by asking.

    Ask question

    Explore related questions

    See similar questions with these tags.