I understand how to use the regular expressions in JS and PHP. I'm trying to switch some of my PHP code to JS and I have a function that goes through different regular expressions to see if it can find a match.
Example PHP:
if(preg_match('/regex1/', $string, $matches)) {
$output = $matches[1];
} else if(preg_match('/regex2/', $string, $matches)) {
$output = $matches[1];
} else etc....
Is there a way to do something similar in JS?
The only way I can think of doing it is:
if(string.match(/regex/)) {
var res = string.match(/regex/);
output = res[1];
} else if ..... and so on
I'd rather not have it do the match twice... Is there another way?
Thanks for your help.