Regex allow digits and a single dot
Asked Answered
H

5

28

What would be the regex to allow digits and a dot? Regarding this \D only allows digits, but it doesn't allow a dot, I need it to allow digits and one dot this is refer as a float value I need to be valid when doing a keyup function in jQuery, but all I need is the regex that only allows what I need it to allow.

This will be in the native of JavaScript replace function to remove non-digits and other symbols (except a dot).

Cheers.

Hoarse answered 6/4, 2011 at 17:54 Comment(0)
K
55

If you want to allow 1 and 1.2:

(?<=^| )\d+(\.\d+)?(?=$| )

If you want to allow 1, 1.2 and .1:

(?<=^| )\d+(\.\d+)?(?=$| )|(?<=^| )\.\d+(?=$| )

If you want to only allow 1.2 (only floats):

(?<=^| )\d+\.\d+(?=$| )

\d allows digits (while \D allows anything but digits).

(?<=^| ) checks that the number is preceded by either a space or the beginning of the string. (?=$| ) makes sure the string is followed by a space or the end of the string. This makes sure the number isn't part of another number or in the middle of words or anything.

Edit: added more options, improved the regexes by adding lookahead- and behinds for making sure the numbers are standalone (i.e. aren't in the middle of words or other numbers.

Krona answered 6/4, 2011 at 18:1 Comment(4)
That regular expression will report a match on pretty much anything. For example, in .NET the result of Regex.Replace("xyzzy", @"\d*(\.\d+)?", "!"); will be !x!y!z!z!y!. I'm pretty sure the results in JavaScript will be the same. Since everything's optional, the regex will match at every position.Trinhtrini
@Jim Mischel, good call. I edited the answer with improved regexes.Mudskipper
@[Harpyon] Is there something missing? I get the message "Invalid group": /(?<=^| )\d+(\.\d+)?(?=$| )|(?<=^| )\.\d+(?=$|)/Wharve
Lookbehinds aren't supported by JavaScript apparently.Emyle
A
27
\d*\.\d*

Explanation:

\d* - any number of digits

\. - a dot

\d* - more digits.

This will match 123.456, .123, 123., but not 123

If you want the dot to be optional, in most languages (don't know about jquery) you can use

\d*\.?\d*
Albaalbacete answered 6/4, 2011 at 17:58 Comment(4)
\D is any character except digits. \d is what you mean.Neufer
\D is the inverse of "match a digit"-- it will match anything but a digit!Goldiegoldilocks
Thank you, @Neufer and @Platinum. I fixed it now. The reason for the mistake is that I copied the \D from the original question. I know in Python \d means "digit" and \D means "anything but a digit", but I thought that if the OP mentioned \D then it must be different in jQuery. According to your comments, apparently my first thought was correct and I should have stuck to it :)Albaalbacete
Getting invalid escape sequence in C#. Any help plzCharinile
S
7

Try this

boxValue = boxValue.replace(/[^0-9\.]/g,"");

This Regular Expression will allow only digits and dots in the value of text box.

Sapele answered 10/10, 2015 at 17:41 Comment(1)
this will allow multiple dots, and question is allow only one dotFormyl
H
3

My try is combined solution.

string = string.replace(',', '.').replace(/[^\d\.]/g, "").replace(/\./, "x").replace(/\./g, "").replace(/x/, ".");
string = Math.round( parseFloat(string) * 100) / 100;

First line solution from here: regex replacing multiple periods in floating number . It replaces comma "," with dot "." ; Replaces first comma with x; Removes all dots and replaces x back to dot.

Second line cleans numbers after dot.

Harmonium answered 7/3, 2016 at 11:24 Comment(1)
Thanks. After searching since 2 Hours, I got this answer. And, this is fitting perfectly in my set of requirements (only numeric digits along with one dot). Thanks Once Again.Razid
E
0

Try the following expression

/^\d{0,2}(\.\d{1,2})?$/.test()
Eringo answered 8/7, 2019 at 5:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.