I just noticed a very strange behaviour. Why does a simple space break all the tests?
rePattern = /^([a-z]+[-_]?){2,}[a-z]$/;
var test = new Array("jhgfg_hfh-g", "jhg-fg_hfhg", "jhg_fg_hfhg", "jhg_fg_hfhg", "jhg_fghfhg");
for (var i = 0; i < test.length ; i++) {
x = test[i];
alert(i + ' : ' + x + ' : ' + rePattern.test( x ));
}
if i change the above to
// notice {2,} => {2, } with an extra space before }
rePattern = /^([a-z]+[-_]?){2, }[a-z]$/;
then everything become false…
thank u
OK, just to have an accepted answer here: it’s because of the extra space. The syntax of regular expressions is strict, you can’t add random whitespace and expect it’ll be ignored.
{2, }will match literal{2, }: