Skip to content

Instantly share code, notes, and snippets.

@sylwiaeb
Created August 15, 2017 20:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sylwiaeb/c54a08701647614fd12f3727fba5478c to your computer and use it in GitHub Desktop.
Save sylwiaeb/c54a08701647614fd12f3727fba5478c to your computer and use it in GitHub Desktop.
[js] extract numbers from text
// converts '7-foo me, 30-boo me, 30-baz me' to '7,30,30'
function serialize(text) {
return text.replace(/[^\d,]/g, '');
}
// converts '7,30,30' to '7-foo me, 30-boo me, 30-baz me'
function humanize(text) {
const words = [
'-foo',
'-boo',
'-baz',
];
arr = text.split(',');
arr.forEach((el, i) => arr[i] = el + words[i]);
return arr.join(', ');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment