Using Regex to Extract Time?

I would like to let users enter a time in a text field, rather than using the date/time picker. I’m trying to use a Regex to pull out the hours, minutes, and am/pm designation from a text field. (Credit to this page for the Regex.) I figured I would have a custom state with a list of texts, and then set that list using the Regex:

However, it only returns 1 item in the list with the full expression. Am I misunderstanding how Bubble uses Regex expressions? Shouldn’t it return a list of expressions?

Hi @kalesalad,

As you noted, it does return a list - a list of all matches found. Since there’s only one time in your string, it’s a list of one. If there were multiple times in the string, there would be multiple items in the list.

As noted in the docs, however, Bubble does not expose capture groups (the subexpressions in parentheses), which I suspect you might be conflating with matches.

BTW, why not just create your own time selection element instead of allowing free-form entry?

1 Like

I agree that Regex seems like overkill here - why not just use an option set? You can set up attributes with number values and convert it to a date at any point.

@kalesalad what sort of problem you are going to solve by allowing user to enter date freely?

This topic was automatically closed after 70 days. New replies are no longer allowed.