I want to know what I'm doing wrong in this code for checking a real number:
var regex = new RegExp(/([0-9]+[\.|,][0-9]*)|([0-9]*[\.|,][0-9]+)|([0-9]+)/g);
var invalid = this.value.match(regex);
The above doesn't work for me while the expression
([0-9]+[\.|,][0-9]*)|([0-9]*[\.|,][0-9]+)|([0-9]+)
works in the tester.
Do
var regex = new RegExp("([0-9]+[.|,][0-9])|([0-9][.|,][0-9]+)|([0-9]+)/g);([0-9]+[.|,][0-9])|([0-9][.|,][0-9]+)|([0-9]+)", 'g');
or
var regex = /([0-9]+[.|,][0-9])|([0-9][.|,][0-9]+)|([0-9]+)/g;
Two constructs are possible : new RegExp(string,'g') or /somestring/g. Don't mix them. In your case of a constant regexp, it will be more efficient to choose the second one because it is precompiled.
See the MDN documentation
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With