In the match
'meeting room 3 @ 5 am - 6 pm bob'
.match(/(@|at)?\s*?(\d+)\s*?(am|pm|AM|PM)?\s*?-\s*?(\d+)\s*?(am|pm|AM|PM)?/)
Why does the last pm
not match?
The result is "["@ 5 am - 6","@","5","am","6",null]"
I expect that null
to be pm
Thanks
Make all the \\s*?
greedy (especially the last one which is the culprit):
/(@|at)?\s*(\d+)\s*(am|pm|AM|PM)?\s*-\s*(\d+)\s*(am|pm|AM|PM)?/
^
See the regex demo
The point is that (\\d+)\\s*?(am|pm|AM|PM)?
matches and captures 1 or more digits with (\\d+)
, then the regex engine tries to match (am|pm|AM|PM)?
pattern, not \\s*?
, because \\s*?
is a lazily quantified atom, and is thus skipped at first. The (am|pm|AM|PM)?
pattern can match an empty string, and it does. It matches the empty string right after the digits, and the regex engine calls it a day returning a valid match.
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.