Luminance
Luminance

Reputation: 918

Non-capturing group still captures input

I have a Date format that can be input in the following format:

December, 13, 2016 
December , 13 , 2016
December    ,13    ,   2016 
.
.
.

So I am trying to match and capture all those input events, to eventually get a result like this:

December 13 2016

My RegEx looks like this:

str.match([a-zA-Z]+(?: |,)*[0-9]{1,2}(?: |,)*[0-9]+$)

Trust me, I have tried searching for a solution all over the web before posting a question, but with no prevail. Any suggestions how to solve this?

Upvotes: 0

Views: 146

Answers (1)

nu11p01n73R
nu11p01n73R

Reputation: 26667

You will have to capture the part that you want to extract. For example,

matches = "December    ,13    ,   2016".match(/([a-zA-Z]+)[ ,]*([0-9]{1,2})[ ,]*([0-9]+)$/)
console.log(matches[1] + ' ' + matches[2] + ' ' + matches[3])
// December 13 2016
  • ([a-zA-Z]+) Captures the month name to matches[1].
  • ([0-9]{1,2}) Captures the date.
  • ([0-9]+) Captures the year.
  • [ ,]* which is same as (?: |,), but without a group using character classes.

Upvotes: 2

Related Questions