Logo

Programming-Idioms

History of Idiom 137 > diff from v27 to v28

Edit summary for version 28 by tom:
[JS] The current implementation will evaluate to true for empty strings. The * character means zero or more.

Version 27

2019-09-26, 13:50:27

Version 28

2019-09-26, 13:52:20

Idiom #137 Check if string contains only digits

Set boolean b to true if string s contains only characters in range '0'..'9', false otherwise.

Idiom #137 Check if string contains only digits

Set boolean b to true if string s contains only characters in range '0'..'9', false otherwise.

Code
var b = /^[0-9]*$/.test(s);
Code
var b = /^[0-9]+$/.test(s);
Comments bubble
Notice the ^ and $ for "beginning of string" and "end of string".
Comments bubble
Notice the ^ and $ for "beginning of string" and "end of string".
Origin
http://stackoverflow.com/questions/1779013/check-if-string-contains-only-digits
Origin
http://stackoverflow.com/questions/1779013/check-if-string-contains-only-digits
Demo URL
https://regex101.com/r/vH3Z59/1/