How can I create a regex that allows whole numbers, decimals, fractions, and fractions with decimals? The string can also have optional text, only at the end. So far I have this:
JavaScript
x
2
1
const re = /^d*.?d*/?d*.?d*[a-z]*$/gi;
2
This allows double decimals in a whole number(ie: ‘23.23.23’), which I do not want. Can I modify this regex to allow two decimal points only if it is separated by a ‘/’?
Here are some examples that can pass:
- 23.23/100km
- 1/3
- .23km
- 1.mi
- 1.2/2.1kg
Some examples that shouldn’t pass:
- 1a3km
- 12.12.12
- 1.2.3/12.13km
- 12km/12.44km
Advertisement
Answer
Use
JavaScript
1
2
1
^(?!.*d+(?:.d+){2})d*.?d*/?d*.?d*[a-z]*$
2
See proof. This expression disallows three numbers that have a period between one another thanks to (?!.*d+(?:.d+){2})
negative lookahead.
EXPLANATION
JavaScript
1
49
49
1
--------------------------------------------------------------------------------
2
^ the beginning of the string
3
--------------------------------------------------------------------------------
4
(?! look ahead to see if there is not:
5
--------------------------------------------------------------------------------
6
.* any character except n (0 or more times
7
(matching the most amount possible))
8
--------------------------------------------------------------------------------
9
d+ digits (0-9) (1 or more times (matching
10
the most amount possible))
11
--------------------------------------------------------------------------------
12
(?: group, but do not capture (2 times):
13
--------------------------------------------------------------------------------
14
. '.'
15
--------------------------------------------------------------------------------
16
d+ digits (0-9) (1 or more times
17
(matching the most amount possible))
18
--------------------------------------------------------------------------------
19
){2} end of grouping
20
--------------------------------------------------------------------------------
21
) end of look-ahead
22
--------------------------------------------------------------------------------
23
d* digits (0-9) (0 or more times (matching
24
the most amount possible))
25
--------------------------------------------------------------------------------
26
.? '.' (optional (matching the most amount
27
possible))
28
--------------------------------------------------------------------------------
29
d* digits (0-9) (0 or more times (matching
30
the most amount possible))
31
--------------------------------------------------------------------------------
32
/? '/' (optional (matching the most amount
33
possible))
34
--------------------------------------------------------------------------------
35
d* digits (0-9) (0 or more times (matching
36
the most amount possible))
37
--------------------------------------------------------------------------------
38
.? '.' (optional (matching the most amount
39
possible))
40
--------------------------------------------------------------------------------
41
d* digits (0-9) (0 or more times (matching
42
the most amount possible))
43
--------------------------------------------------------------------------------
44
[a-z]* any character of: 'a' to 'z' (0 or more
45
times (matching the most amount possible))
46
--------------------------------------------------------------------------------
47
$ before an optional n, and the end of the
48
string
49