Javascript String toCents()
// http://www.codewars.com/kata/regexp-basics-parsing-prices String.prototype.toCents = function() { if (typeof +this !== 'number') return null; let m = this.valueOf().match(/^[$](\d+)\.(\d{2})$/); if (!m || !m.length) return null; return +`${m[1]}${m[2]}`; }
//Implement String#to_cents, which should parse prices expressed //as $1.23 and return number of cents, or in case of bad format return nil. String.prototype.toCents = function(){ return /^\$\d*\.\d{2}$/.test(this) ? +this.replace(/[\$\.]/g, '') : null; };