1

I am trying to create a Regex for matching version number which can be in format as 1.2.3.4 It can also contain only one number like 12 or it can also contain *(asterisk) instead of numbers.

I tried creating this as follows

[\d*]*\.[\d*]*\.[\d*]*\.[\d*]*

This works to some extent, but it needs to have exactly in that format ie it needs to have all the decimal points whereas what am looking for is to allow any number and * in the version type.

Invalid scenarios can be

1.2.4.5.6.
.
3.4.

Valid scenarios are

12
*
*.*
12.34.5.*
1.4.5.6
3.*.*

Any help in this ?

Thanks

1 Answer 1

2

You may use this regex in Javascript:

^(?:\d+|\*)(?:\.(?:\d+|\*))*$

RegEx Demo

RegEx Details:

  • ^: Start
  • (?:\d+|\*): Match 1+ digits or *
  • (?:\.(?:\d+|\*))*: Match a group with dot followed by 1+ digits or *. This group can match 0 or more times.
  • $: End
Sign up to request clarification or add additional context in comments.

1 Comment

thanks for your reply. Can we not allow more then 3 decimals ? right now it doesnt show error for numbers like 1.2.3.4.5.6.7

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.