Skip to content

Instantly share code, notes, and snippets.

@benshimmin
Created January 14, 2013 05:55
Show Gist options
  • Save benshimmin/4527992 to your computer and use it in GitHub Desktop.
Save benshimmin/4527992 to your computer and use it in GitHub Desktop.
Neatest way of getting the digits out of a string
# 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