Skip to content

Instantly share code, notes, and snippets.

@wcoder
Last active September 23, 2017 13:35
Show Gist options
  • Save wcoder/f1cba160411340654a1b520da417fffb to your computer and use it in GitHub Desktop.
Save wcoder/f1cba160411340654a1b520da417fffb to your computer and use it in GitHub Desktop.
// (c) 2016 Yauheni Pakala
var _parseInt = str => str.replace(/(.*?)(\d+)(.*)/,'$2') - 0;
// test
console.log(_parseInt("123hui"));
console.log(_parseInt("123hui321"));
console.log(_parseInt("hui123"));
// time
console.time("parseInt");
parseInt("123qwe");
console.timeEnd("parseInt");
console.time("_parseInt");
_parseInt("123qwe");
console.timeEnd("_parseInt");
// result:
// parseInt: 0.005126953125ms
//_parseInt: 0.00390625ms
@wcoder
Copy link
Author

wcoder commented Sep 23, 2017

function toNumber(strNumber) {  
   return +strNumber;
}

// toNumber("123") -> 123
// toNumber("asd") -> NaN
// toNumber("2as") -> NaN

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment