JavaScript match portions of an optional section

I have a regex set up to match a US-formatted date and time. It looks like this:

/(\d{1,2})\/(\d{1,2})\/(\d{2,4}) (\d{1,2}):(\d{1,2})(am|pm|AM|PM|Am|Pm)/

However, I need it to also match dates that do not contain the time component.

How do I modify this, so that if it's a date, I get 3 matches, and if it's a date-time, I get six?

Answer:1

Group the time and make it optional:

/(\d{1,2})\/(\d{1,2})\/(\d{2,4})(?: (\d{1,2}):(\d{1,2})(am|pm|AM|PM|Am|Pm))?/

The ?: suppresses capturing, so that you don't end up with an additional group if the time is present.

Also, if you're not too picky about allowing aM and pM, you can simplify considerably with a case-insensitive modifier:

/(\d{1,2})\/(\d{1,2})\/(\d{2,4})(?: (\d{1,2}):(\d{1,2})([ap]m))?/i

In fact, using ? you can even shorten the pattern further, (because \d? is shorter than {1,2}:

/(\d\d?)\/(\d\d?)\/(\d{2,4})(?: (\d\d?):(\d\d?)([ap]m))?/i

Note however, that in any case you will still get 6 groups (7 if you count the overall match) - it's just that the last three will be undefined:

> groups = '11/11/11'.match(/(\d{1,2})\/(\d{1,2})\/(\d{2,4})(?: (\d{1,2}):(\d{1,2})([ap]m))?/i)
["11/11/11", "11", "11", "11", undefined, undefined, undefined]

The same happens when using exec instead. But these are easy to filter out if necessary:

> groups.filter(function(capture) {
      return typeof capture !== 'undefined';
  });
["11/11/11", "11", "11", "11"]

or in this case (because you can never have empty strings as captures):

> groups.filter(function(capture) {
      return capture;
  });
["11/11/11", "11", "11", "11"]
Answer:2

Use ? around time regex, wrapper with (?:) non-capturing group.

/(\d{1,2})\/(\d{1,2})\/(\d{2,4})(?:\s+(\d{1,2}):(\d{1,2})(am|pm|AM|PM|Am|Pm))?/

Note that literal space has been replaced with \s+.

+ - match 1 or more

* - match 0 or more

? - match 0 or 1

Answer:3

I am attempting to render a map of Brooklyn's building and lots using D3.js. To accomplish this task, I have done the following: Obtained shapefile from MapPLUTO: http://www.nyc.gov/html/dcp/html/...

I am attempting to render a map of Brooklyn's building and lots using D3.js. To accomplish this task, I have done the following: Obtained shapefile from MapPLUTO: http://www.nyc.gov/html/dcp/html/...

I'm building a web app where users can connect to each other and chat. So each chat holds two users. I'm having some general questions concerning Pusher API, (http://pusher.com). First question. ...

I'm building a web app where users can connect to each other and chat. So each chat holds two users. I'm having some general questions concerning Pusher API, (http://pusher.com). First question. ...

  1. pusher channels react
  2. pusher channels pricing
  3. pusher channels vs beams
  4. pusher channels vs chatkit
  5. pusher channels flutter
  6. pusher channels docs
  7. pusher channels websocket
  8. pusher channels name
  9. pusher channels methods
  10. pusher private channels
  11. pusher multiple channels
  12. pusher presence channels
  13. laravel pusher channels
  14. pusher max channels
  15. pusher js channels
  16. authenticated pusher channels
  17. using pusher channels
  18. pusher node js channels
  19. pusher subscribe multiple channels
  20. pusher vs django channels

I have a list of bootstrap component class="list-group" I have a folder of videos/category, I want to when I click on a category, the list updates without loading a page (by changing the path). Have ...

I have a list of bootstrap component class="list-group" I have a folder of videos/category, I want to when I click on a category, the list updates without loading a page (by changing the path). Have ...

  1. dynamic list html javascript
  2. dynamic list html
  3. dynamic list html jquery
  4. dynamic list html c#
  5. dynamic drop down list html
  6. dynamic search list html
  7. create dynamic list html
  8. dynamic select list html
  9. dynamic checkbox list html
  10. dynamic unordered list html
  11. dynamic list filter html
  12. dynamic list display html
  13. dynamic list data html
  14. dynamic drop down list html form
  15. dynamic drop down list html javascript
  16. dynamic drop down list html jquery
  17. dynamic list view in html
  18. dynamic drop down list html python
  19. dynamic drop down list html php
  20. dynamic drop down list html w3schools

I have a object in my HTML site which I expand/add to with a little JavaScript bit (below) on button click. Can I refresh the dropdown/select on it's own to show the new choices automaticaly or can ...

I have a object in my HTML site which I expand/add to with a little JavaScript bit (below) on button click. Can I refresh the dropdown/select on it's own to show the new choices automaticaly or can ...