0

I need to compare text ends with "AM" using regex. With case insensitive input i.e.

  • AM
  • am
  • Am
  • aM

All should be valid scenario. I am trying below

myinput.match(/^\w*am\b$/)

But it is doing case sensitive comparison (i.e. for 'am' but not working for AM, Am, aM).

JSFiddle

1 Answers1

2

You need to add case intensive flag at the end:

myinput.match(/^\w*am\b$/i)

Edit: as per your comment

The issue with 07.15am is the first part of your regex. If you want to be strict then you could try:

/^(\w+)(\.\w+)\s?(am)$/i

This will match 07.15am, 07.15Am or 07.15 AM.

If you want to be a little looser you can make the second group optional.

/^(\w+)(\.\w+)?\s?(am)$/i

Also this wont work if you have anything before or after the string. I would suggest removing the ^$ boundaries.

/(\w+)(\.\w+)?\s?(am)/i

That should cover most cases.

Richard Vanbergen
  • 1,904
  • 16
  • 29