Reputation: 11835
i get an strange error "invalid quantifer" ... can somebody help me please?
html
<input type="text" value="5+5" id="test"/>
JS
ups = {};
ups['2'] = new Array();
ups['2']['cmd'] = '#(\-|\+|\*|\/|)[0-9](,|[0-9]|)(\-|\+|\*|\/)[0-9]#gi';
var inp_val = $('#test').val();
if (inp_val.match(ups['2']['cmd']))
{
$('body').append('<br />OK');
}
http://www.jsfiddle.net/V9Euk/639/
Upvotes: 1
Views: 795
Reputation: 523374
In Javascript, Regular expressions should be written as
var re = /thepatterns/mod;
i.e.
ups = {};
ups['2'] = new Array();
ups['2']['cmd'] = /(\-|\+|\*|\/|)[0-9](,|[0-9]|)(\-|\+|\*|\/)[0-9]/gi;
moreover, it can be simplified as
ups = {2: {cmd: /([-+*\/])\d([,\d]?)([-+*\/])\d/g }};
The reason for the invalid quantifier is that, unlike PHP, a \
followed by an unrecognized character will result in that character without the \
:
'\p\q' == 'pq'
'\p\q' == '\\p\\q'
Therefore, your string will be interpreted as
'#(-|+|*|/|)[0-9](,|[0-9]|)(-|+|*|/)[0-9]#gi'
which is an invalid regex since the +
(a quantifier) is not preceded by any patterns.
Upvotes: 4