简体   繁体   中英

Regex to match version number in Javascript

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM