Skip to content

Instantly share code, notes, and snippets.

@lsauer
Created May 20, 2012 07:18
Show Gist options
  • Save lsauer/2757164 to your computer and use it in GitHub Desktop.
Save lsauer/2757164 to your computer and use it in GitHub Desktop.
A way around the JavaScript RegExp match null issue + Example of counting a letter in a string
//www.lsauer.com - lo sauer 2012
//description: JavaScript's match returns null when no matches are found.
// 'null' has no Array methods resulting in error's should it be called directly
//#Example: count the occurance of 'o' in a string
// (incidentally this should be the fasted method available of counting a letter in a string)
("this is foo".match(/o/g)||[]).length
//>2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment