SeaBass
SeaBass

Reputation: 1764

Filter version number from string in javascript?

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.

Upvotes: 0

Views: 490

Answers (3)

The fourth bird
The fourth bird

Reputation: 163362

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

Upvotes: 1

Toto
Toto

Reputation: 91428

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

Upvotes: 0

vrintle
vrintle

Reputation: 5586

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

    Upvotes: 0

    Related Questions