title: Split a Date/Time String into Tokens with Regex
body: <p>I am trying to break a date format string (and an input date string) into parts (tokens).</p>
  
  <p>A date format can use any separating character between tokens, as long as the tokens are valid. The date/time will be parsed into the format and the non-alphanumeric characters will carry over into the output string. I would like to strip the tokens from both the date/time format string and the input date/time string.</p>
  
  <p><strong>I need to split a string on all sets of non-alphanumeric characters</strong></p>
  
  <p>For example, let's say my date format string is:</p>
  
  <pre><code>'dddd, MMMM D, YYYY h:mm A'
  </code></pre>
  
  <p>I would like to have an array of all of the date/time tokens in the string. My output should be:</p>
  
  <pre><code>['dddd', 'MMMM', 'D', 'YYYY', 'h', 'mm', 'A']
  </code></pre>
  
  <p>I tried a couple of attempts, but I still cannot wrap my head around how to do this. I was able to come up with the regex:</p>
  
  <pre><code>[^\w]|[_]
  </code></pre>
  
  <p>which should match any character not present in <code>[a-zA-Z0-9_]</code> as the first alternative or match <code>_</code> literally as the second alternative.</p>
  
  <p>I have tested this regex and it does work, yet how can I use it properly?</p>
  
  <p>I tried to use:</p>
  
  <pre><code>var format_tokens = display_format.match(/[^\w]|[_]/g);
  </code></pre>
  
  <p>..which, of course, returned me:</p>
  
  <pre><code>format_tokens = [",", " ", " ", ",", " ", " ", ":", " "];
  </code></pre>
  
  <p>..which should have been obvious...</p>
  
  <p>So how do I get the inverse? How can I get the desired result of:</p>
  
  <pre><code>format_tokens = ['dddd', 'MMMM', 'D', 'YYYY', 'h', 'mm', 'A']
  </code></pre>
  
  <p>?</p>
  
  <p>Thanks everybody!</p>