Regex get last two digits of year

前端 未结 3 1469
-上瘾入骨i
-上瘾入骨i 2021-01-24 20:51

I must use regex in order to get the last two digits of a year but only when 4 digits exist. I have the following regex which works perfectly when there is 4 digits. Example 20

3条回答
  •  生来不讨喜
    2021-01-24 21:39

    You can use this regex.

    ^(?(?=\d{4}$)..(\d{2}))$
    

    Working demo

    This regex uses an IF clause, so if the string is 4 digits then captures the last two.

    enter image description here

提交回复
热议问题