I am trying to capture the bold part of strings like this:
‘capture a year range at the end of a string 1995-2010‘
‘if there’s no year range just capture the single year 2005‘
‘capture a year/year range followed by a parenthesis, including the parenthesis 2007-2012 (58 months)‘
This regex works for 1 and 2, but I can’t get it to work for 3:
/(\d+([-–— ]\d+( \(\d+ months\))?)?$)/
What am I doing wrong?
Try this regex:
This one captures everything in the brackets.
If you need a regex specific to the text “(number) months” in the brackets, then you can use this:
\d{4}(?:[-–— ]\d{4})?(?:\s+\(\d+\smonths\))?$Link to test: RegexPal or RegExr
Sample text:
parenthesis 2007-2012 (58 months)
And the JavaScript code: