Consider the following regex code snippet in Perl:
if ($message =~ /^(.+) enters the race!$/)) {
$racer = $1;
print "Found $racer";
} else {
print "Racer parsing error!";
}
I'm trying to port this to JavaScript, and here's what I have come up with:
if (message.match(/^.+ enters the race!$/)) {
var racer = message.match(/^(.+) enters the race!$/)[1];
console.log("Found " + racer);
} else {
console.log("Racer parsing error!");
}
Notice how the regex has to be repeated twice. This look sloppy. Not to mention that it wastes processing power since it has to do the same regex twice in a row. Is there any way to make this code snippet look cleaner?
if (racer = message.match(/^(.+) enters the race!$/)[1] ) { ..} else { ..}