javascript - What is the regex for “Any positive integer, excluding 0” -


there other post can't comment on it. sorry that.

i used

var pattern = new regexp('^[1-9]\d*$');  var result = fieldvalue.search(pattern); 

but "-1" if put 12

it allows me number 1 9 , no more.

is there wrong?

assuming language javascript, need escape backslash character within string have value of backslash:

'\d' string value of d
'\\d' string value of \d

var pattern = new regexp('^[1-9]\\d*$'); 

javascript has regular expression literals, avoid need additional escape characters:

var pattern = /^[1-9]\d*$/; 

Comments