I'm trying to use a JavaScript regular expression to check a string for anything that matches a certain set of letters followed by either 5 or 6 numbers. For example, if the string is 'MyValue12345 blah blah MyValue123456 blah MyValue222221'
, it should return a match of ['MyValue12345', 'MyValue123456', 'MyValue222221']
. I'm a RegEx novice (clearly), and my expression gets close but not exactly what I'm looking for...
Here is my first try:
var str = 'MyValue12345 blah blah MyValue123456 blah MyValue222221';
var matchArr = str.match(/MyValue\d\d\d\d\d|MyValue\d\d\d\d\d\d/gi);
matchArr
then equates to ['MyValue12345', 'MyValue123456', 'MyValue22222']
, but the last value of MyValue22222
isn't what I'm looking for, I want it to say the full expression of MyValue222221
. I know there's a better way to do this but I'm struggling to get it...any thoughts?