How do I specify optional characters in a regular expression?

I have this regex.

MMM D[,] YYYY /^(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)\ (\d{2})\, (\d{4})$/ 

How can I do this [,] is optional?

 MMM D, YYYY MMM D YYYY 
+4
source share
3 answers

just do ,? , which makes the comma optional.

+6
source

Did not check the rest of the expression, but placing ? after the decimal point, let's say it is 0 or 1 time. If you want to allow multiple commas, you must use * , from 0 to infinity.

+5
source

The question mark after the character makes it optional in the regular expression.

+3
source

All Articles