Skip to content

Instantly share code, notes, and snippets.

@krohne
Created March 14, 2018 22:32
Show Gist options
  • Save krohne/6c76353864203f70dcff2f3e655374e5 to your computer and use it in GitHub Desktop.
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
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