Regex to check whether a string contains only numbers

As you said, you want hash to contain only numbers.

const reg = new RegExp('^[0-9]+$');

or

const reg = new RegExp('^\d+$')

\d and [0-9] both mean the same thing. The + used means that search for one or more occurring of [0-9].


var reg = /^\d+$/;

should do it. The original matches anything that consists of exactly one digit.


This one will allow also for signed and float numbers or empty string:

var reg = /^-?\d*\.?\d*$/

If you don't want allow to empty string use this one:

var reg = /^-?\d+\.?\d*$/