javascript regex validate years in range

后端 未结 8 1113
猫巷女王i
猫巷女王i 2021-02-13 20:11

I have input field for year and I need a regex for validation it. I have such code: ^([12]\\d)?(\\d\\d)$. But I want allow to validate only years in certain range (

8条回答
  •  爱一瞬间的悲伤
    2021-02-13 20:30

    Here is a regex if you want to find a year in a film name for example. Years b/n 1900 - 2029 and some symbols are allowed wrapping the year .-_+[(

    (?<=(?:\s|\.|_|\-|\+|\(|\[))(?:19[2-9]|20[0-2])\d(?=\s|$|\.|_|\-|\+|\)|\])
    

    check it out here https://regex101.com/r/eQ9zK7/82

    Note you can not start with year, because there is at least interval in front. In the example first few lines are matching, because we have multiple lines in a single line they wont match.

    1917.2019.1080p... even if 1917 was in range it will mark only 2019

提交回复
热议问题