R01010010
R01010010

Reputation: 5968

Regular expression for a real number with comma or dot for JavaScript

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.

Upvotes: 3

Views: 9354

Answers (3)

GottZ
GottZ

Reputation: 4947

first of all, you do not need to do new RegExp() on a regular expression that is not within a string.

/regexp/rule OR new RegExp("regexp", "rule");

secondly:

why do you use [0-9] if you could just use \d?

third:

why do you use [.|,]? do you want to match | aswell? [.,] would do the job you want to achieve.

fourth:

check this against a numerical string: /^(?:[1-9]\d{0,2}(?:,\d{3})*|0)(?:\.\d+)?$/

you can do like this:

var regexp = /^(?:[1-9]\d{0,2}(?:,\d{3})*|0)(?:\.\d+)?$/;

alert(regexp.test("0")); // true
alert(regexp.test("1")); // true
alert(regexp.test("01")); // false (or check out the regex at the bottom)
alert(regexp.test("123")); // true
alert(regexp.test("1234")); // false
alert(regexp.test("123,4")); // false
alert(regexp.test("123,456,789,012")); // true
alert(regexp.test("123,456,789,012.")); // false
alert(regexp.test("123,456,789,012.12341324")); // true
alert(regexp.test("0.12341324")); // true

in case you do want to match something like 0,000,000.0000 aswell you could use this regex:

/^\d{1,3}(?:,\d{3})*(?:\.\d+)?$/

in case you want +- in front you can add what Bergi mentioned. my regex would then look like this:

/^[+-]?\d{1,3}(?:,\d{3})*(?:\.\d+)?$/

or: /^[+-]?(?:[1-9]\d{0,2}(?:,\d{3})*|0)(?:\.\d+)?$/

as Bergi mentioned you should know how to have . as grouping operator and , as delimiter. for that you just need to replace , with \. and \. with ,

there are the expressions with replaced , and .

/^?\d{1,3}(?:\.\d{3})*(?:,\d+)?$/ <- matches 00,000,000.00000
/^?(?:[1-9]\d{0,2}(?:\.\d{3})*|0)(?:,\d+)?$/ <- matches 1,123,123,123.1234
/^[+-]?\d{1,3}(?:\.\d{3})*(?:,\d+)?$/ <- matches -00,000.0
/^[+-]?(?:[1-9]\d{0,2}(?:\.\d{3})*|0)(?:,\d+)?$/ <- matches -12,123.12345

Upvotes: 3

Bergi
Bergi

Reputation: 665536

I'd suggest this:

/[+-]?(?:\d*[.,])?\d+/

It uses the shortcut \d instead of [0-9], also I don't think you want to match the pipe as a decimal delimiter. Square brackets define a character class, in which special chars loose their meaning (. doesn't need escaping, | doesn't mean OR) - you probably meant (\.|,). Also I'm not sure whether you really want to match floas without decimal digits (e.g. "12,") - I've omitted them; and I've allowed an optional sign in the beginning.

Upvotes: 1

Denys S&#233;guret
Denys S&#233;guret

Reputation: 382464

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

Upvotes: 5

Related Questions