Reputation: 450
I am new to regex and I want to validate create a validator pattern for number and only one string (M|m|T|t|B|b).
Here is the one I have tried /^\d*|(M|m|T|t|B|b){1}
But I am not able to create it. These are the test criteria I want to pass
1234b > pass
1234B > pass
.124b > pass
0.123 > pass
1234bb > fail
12345e > fail
Can anyone help?
Upvotes: 0
Views: 468
Reputation: 10360
Try this regex:
(?:\d*\.\d+|\d+)[MTBmtb]{0,1}\b
Explanation:
\d*\.\d+
- matches 0+ digits followed by a decimal followed by 1+ occurrences of a digit|
- OR\d+
- matches 1+ digits[MTBmtb]{0,1}
- matches 0 or 1 occurrence of either of the the letters - M
, T
, B
, m
, t
, b
\b
- a word boundaryUpvotes: 1