I do not understand why undefined
appears in result. I use a question mark (\d+)?
, it matches the previous element zero or one time:
// I want to match two cases with one regexp:
// "GBP 5 per hour" and "GBP 5 per 10 hours"
//
"GBP 5 per hour".match(/([a-z]{3}) (\d+(?:\.\d+)?) per\s?(\d+)?\s?(hours?|days?|minutes?)/im)
["GBP 5 per hour", "GBP", "5", undefined, "hour"] // actual result
["GBP 5 per hour", "GBP", "5", "hour"] // the result I'd like to get
How to get rid of that undefined
?