I am trying to match these use cases in Javascript:
a) 8 Months
b) 18 Years
Numbers could range from 1 to 20 and the strings could only be either "months" or "years"
I am trying this
[0-9]\s(Years|Months)
But it is not working.
I am trying to match these use cases in Javascript:
a) 8 Months
b) 18 Years
Numbers could range from 1 to 20 and the strings could only be either "months" or "years"
I am trying this
[0-9]\s(Years|Months)
But it is not working.
You can use this:
([1-9]|1[0-9]|20)\s(Years|Months)
where:
[1-9] matches a digit from 1 to 91[0-9] matches a number from 10 to 1920 matches just 20Edit: As noticed in a comment, you should use
^([1-9]|1[0-9]|20)\s(Years|Months)$
if the whole string must exactly match with this text.
Another option is prepending the regex with a word boundary (\b) in order to prevent matching cases like "42 Years".
2 Years in the text 42 Years, depending on the context of OP you (the reader of the answer) might want to anchor the regex to the start ^ and end $ of the string or use a negative lookbehind to make sure the character before the match is not a number.