Javascript number regex test failing - can't figure out why -
here's jsfiddle: jsfiddle
relevant code:
var number = /\d+(.\d+)?/g; $('body').append(number.test(2.5) + "<br>"); $('body').append(number.test(20) + "<br>"); $('body').append(number.test(2) + "<br>");
output
true false true
get rid of g
. it's not needed, , causes regex fail because regexp object keeps track of position based on previous match: https://developer.mozilla.org/en-us/docs/javascript/reference/global_objects/regexp/test
as exec (or in combination it), test called multiple times on same global regular expression instance advance past previous match.
Comments
Post a Comment