Skip to main content
added 377 characters in body
Source Link
Roko C. Buljan
  • 209k
  • 41
  • 335
  • 346
function hasNumber( str ){ return /\d/.test( str ); } 

results:

hasNumber(0) // true hasNumber("0") // true hasNumber(2) // true hasNumber("aba") // false hasNumber("a2a") // true 

While the above will return *truthy* as soon the function encounters a (simgle)**one** *Number* `\d` if you want to be able return**return an Array of the matched resultsresults**:
function hasNumber( str ){ return str.match( /\d+/g ); } 

results:

hasNumber( "11a2 b3 c" ); // ["11", "2", "3"] hasNumber( "abc" ); // null if( hasNumber( "a2" ) ) // "truthy statement" 

where + in /\d+/g is to match one or more Numbers (therefore the "11" in the above result) and the g Global RegExp flag is to continue iterating till the end of the passed string.

Advanced_Searching_With_Flags
RegExp.prototype.test()
String.prototype.match()

function hasNumber( str ){ return /\d/.test( str ); } 

results:

hasNumber(0) // true hasNumber("0") // true hasNumber(2) // true hasNumber("aba") // false hasNumber("a2a") // true 

While the above will return *truthy* as soon the function encounters a (simgle) *Number* if you want to be able return an Array of the matched results:
function hasNumber( str ){ return str.match( /\d+/g ); } 

results:

hasNumber( "11a2 b3 c" ); // ["11", "2", "3"] hasNumber( "abc" ); // null 
function hasNumber( str ){ return /\d/.test( str ); } 

results:

hasNumber(0) // true hasNumber("0") // true hasNumber(2) // true hasNumber("aba") // false hasNumber("a2a") // true 

While the above will return *truthy* as soon the function encounters **one** *Number* `\d` if you want to be able **return an Array of the matched results**:
function hasNumber( str ){ return str.match( /\d+/g ); } 

results:

hasNumber( "11a2 b3 c" ); // ["11", "2", "3"] hasNumber( "abc" ); // null if( hasNumber( "a2" ) ) // "truthy statement" 

where + in /\d+/g is to match one or more Numbers (therefore the "11" in the above result) and the g Global RegExp flag is to continue iterating till the end of the passed string.

Advanced_Searching_With_Flags
RegExp.prototype.test()
String.prototype.match()

added 377 characters in body
Source Link
Roko C. Buljan
  • 209k
  • 41
  • 335
  • 346
function hasNumber( str ){ return /\d/.test( str ); } 

resultsresults:

hasNumber(0) // true hasNumber("0") // true hasNumber(2) // true hasNumber("aba") // false hasNumber("a2a") // true 

While the above will return *truthy* as soon the function encounters a (simgle) *Number* if you want to be able return an Array of the matched results:
function hasNumber( str ){ return str.match( /\d+/g ); } 

results:

hasNumber( "11a2 b3 c" ); // ["11", "2", "3"] hasNumber( "abc" ); // null 
function hasNumber( str ){ return /\d/.test( str ); } 

results

hasNumber(0) // true hasNumber("0") // true hasNumber(2) // true hasNumber("aba") // false hasNumber("a2a") // true 
function hasNumber( str ){ return /\d/.test( str ); } 

results:

hasNumber(0) // true hasNumber("0") // true hasNumber(2) // true hasNumber("aba") // false hasNumber("a2a") // true 

While the above will return *truthy* as soon the function encounters a (simgle) *Number* if you want to be able return an Array of the matched results:
function hasNumber( str ){ return str.match( /\d+/g ); } 

results:

hasNumber( "11a2 b3 c" ); // ["11", "2", "3"] hasNumber( "abc" ); // null 
deleted 1 character in body
Source Link
Roko C. Buljan
  • 209k
  • 41
  • 335
  • 346
function hasNumber( str ){ return /\d+\d/.test( str ); } 

results

hasNumber(0) // true hasNumber("0") // true hasNumber(2) // true hasNumber("aba") // false hasNumber("a2a") // true 
function hasNumber( str ){ return /\d+/.test( str ); } 

results

hasNumber(0) // true hasNumber("0") // true hasNumber(2) // true hasNumber("aba") // false hasNumber("a2a") // true 
function hasNumber( str ){ return /\d/.test( str ); } 

results

hasNumber(0) // true hasNumber("0") // true hasNumber(2) // true hasNumber("aba") // false hasNumber("a2a") // true 
Source Link
Roko C. Buljan
  • 209k
  • 41
  • 335
  • 346
Loading