Created
January 14, 2013 05:55
-
-
Save benshimmin/4527992 to your computer and use it in GitHub Desktop.
Neatest way of getting the digits out of 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
# It's very common to have a class with a name like "button-23" or | |
# "container2346112" and to need to extricate just the digits. Of course | |
# this is simple, but I've seen some developers do this in weird and | |
# wonderful ways. Here's a simple way: | |
String::toDigits = -> parseInt @.replace(/\D+/g, ""), 10 | |
# Or in JavaScript: | |
`String.prototype.toDigits = function() { return parseInt(this.replace(/\D+/, ""), 10); }` | |
# Thus: | |
# "abc123def456".toDigits() => 123456 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment