I have this regular expression for finding time:
/ (\[|\(|)([0-1]?[0-9]|2[0-3])(:)[0-5][0-9](:[0-5][0-9])?(( ?am| ?pm|))(\]|\)|)/gi
Currently, it correctly identifies:
11:32
11:32 am
11:32 Am
11:32 AM
(11:32)
[11:32 am]
The only thing that doesn’t work is that it includes the right bracket even if the left bracket is not there, for example it would include:
11:32)
Which I don’t want.
How can I solve this issue?
>Solution :
I’ve "simplified" your question to match:
x
(x)
[x]
while ignoring:
x)
x]
(x
[x
[x)
(x]
I’ve come up with the following:
(?:^(?:(?=[^)\]]+$)|\[(?=.*\]$)|\((?=.*\)$)))x[)\]]?$
See regex101‘s demo, and Debuggex‘s diagram:
A tad hairy!
