I have a javascript string variable like so:
var somestring = "mypattern_var1_var2";
How can I use regex to extract var1 & var2?
No need
var parts = somestring.split("_");
alert(parts[1] + ':' + parts[2])
Or get real fancy like How can I match multiple occurrences with a regex in JavaScript similar to PHP's preg_match_all()?
And we MUST have jQuery in the equation: Regular expression field validation in jQuery
each ;)You could use a split() in this situation - given that you always know that you will have a pattern in the beginning, (given that the pattern doesn't contain any '_'s)
var somestring = "mypattern_var1_var2";
var example = somestring.split('_');
var1 = example[1];
var2 = example[2];