Javascript String digit()
/*/*w ww . j a v a 2 s .co m*/ Implement String#digit? (in Java StringUtils.isDigit(String)), which should return true if given object is a digit (0-9), false otherwise. */ String.prototype.digit = function() { return /^\d$/i.test(this); };
//Implement String#digit? (in Java StringUtils.isDigit(String)), //which should return true if given object is a digit (0-9), false otherwise. String.prototype.digit = function() { return this.match(/^[0-9]$/) ? true : false; };
String.prototype.digit = function() { var reg = new RegExp('^[0-9]$'); console.log(this,reg);/*from w w w . j av a 2 s . co m*/ return reg.test(this); };
String.prototype.digit = function () { return /^\d$/.test(this) && this < 10; }; console.log("2".digit());
String.prototype.digit = function() { if (this.length!=1) return false; if (this[0]>='0'&&this[0]<='9') return true;//from w w w . j a v a2s. co m else return false; };
/*/*from w w w . j a v a 2 s . c om*/ Description: Implement String#digit? (in Java StringUtils.isDigit(String)), which should return true if given object is a digit (0-9), false otherwise. */ String.prototype.digit = function() { return /^\d$/g.test(this); };
String.prototype.digit = function() { return /^\d$/.test(this); }; // mine is below/* ww w. ja v a 2 s . c om*/ String.prototype.digit = function() { if (/[a-z\s]/gi.test(this)) return false; if (this>9) return false; return /[0-9]/g.test(this); }; ''.digit() // , false); '7'.digit() // , true); ' '.digit() //, false); '14'.digit() //, false);
console.clear();//from ww w .j av a2s . c om /* ----------------------------------------- 8 kyu Regexp Basics - is it a digit? ----------------------------------------- Create a string constructor method called digit that should return true if given object is a digit (0-9), false otherwise. */ // -------------------------------------- String.prototype.digit = function() { return /^\d$/.test(this); }; var size = '9' console.log( size.digit() ); // -------------------------------------- String.prototype.digit2 = function() { return /^[0-9]$/.test(this); }; // you can add global flag like this return /^\d$/g.test(this);