I need to capture a value based on a pattern match, but also based on a priority. Thinking of an If Then Else.
ABC-10R0349
CBS-19K49
NBC-20R1529
PBS-20-0524
For example I need to return CBS-19K49 or first match using ([a-zA-Z]{3}-[A-Za-z0-9]{5}) if it is available, but if it is not available then return ABC-10R0349 or first match using ([a-zA-Z]{3}-[A-Za-z0-9]{7})
Eventually I may need to add another match such as PBS-20-0524 or first match using ([a-zA-Z]{3}-[0-9]{2}-[A-Za-z0-9]{4})
Can’t quite get what I’m looking for. (?:(?=[a-zA-Z]{3}-[A-Za-z0-9]{5,}$)?([a-zA-Z]{3}-[A-Za-z0-9]{5})|([a-zA-Z]{3}-[A-Za-z0-9]{7}))
I honestly don’t think this can be done in a single RegEx expression: you want to look ahead and look back simultaneously, only capturing the first instance of a group that may or may not have been captured already…
It would be way easier (and much more manageable!) to just script it with 3 separate RegExes:
var r1 = /^[a-z]{3}-[a-z0-9]{5}$/gmi;
var r2 = /^[a-z]{3}-[a-z0-9]{7}$/gmi;
var r3 = /^[a-z]{3}-[a-z0-9]{2}-[a-z0-9]{4}$/gmi;
Run the first one, check for matches. If none, run the second one, etc.