Created
March 14, 2018 22:32
-
-
Save krohne/6c76353864203f70dcff2f3e655374e5 to your computer and use it in GitHub Desktop.
Force string to numeric-only string that can be converted to integer by replacing non-digits with integer character code
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
function toDigits (str = '') { | |
// replace every (/g) non-digit (\D) with the character code (charCodeAt) for that non-digit. | |
return str.replace(/\D/g, nondigit => nondigit.charCodeAt(0) ); | |
} | |
console.log(toDigits('A1b2C3-4'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment