Expert Refresh

Some examples

1) Regex to match strings within double-quotes




2) Regex to match AM hour time (0 to 12)






Find variable names
Variable names are allowed to contain only alphanumeric characters and underscores, but not begin with a digit. Regex /[a-zA-Z_][a-zA-Z_0-9]*/
Find string with double-quotes
We match all characters except a double quote, and apply star quantifier (any number of such non double-quote characters). Regex /"[^"]"/
Time of the day 9:17 AM or 12:30 PM
Something like /[0-9]?[0-9]:[0-9][0-9]/ matches 9:17 or 12:30, but also allows 99:99 Looking at the hour, we realize that if it is a two-digit number, the first digit must be 1. But, 1?[0-9] still allows 19, which is wrong. We can use alternation for the hour part ([1-9]|1[012]). The correct regex is /([0-9]|1[012]):[0-5][0-9] (am|pm)/
Time of the day 00:01 to 23:59
Regex is /[01]?[0-9]|2[0-3]:[0-5][0-9]/


References