Created
May 20, 2012 07:18
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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