Skip to content

Instantly share code, notes, and snippets.

@mattryall
Created October 17, 2011 06:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mattryall/1292030 to your computer and use it in GitHub Desktop.
Save mattryall/1292030 to your computer and use it in GitHub Desktop.
Regex fall-throughs using if/else, inspired by simplicity and clarity
[17:05] node-test$ node switch.js for the fail
fail
[17:05] node-test$ node switch.js for the lose
lose
[17:05] node-test$ node switch.js bleep blah
blah
[17:05] node-test$ node switch.js for the win
win
var m, s = process.argv.slice(2).join(' ');
if (m = s.match(/fail/)) {
console.log('fail');
} else if (m = s.match(/for the (\S+)/)) {
console.log(m[1]);
} else if (m = s.match(/blah/)) {
console.log('blah');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment