Last active
September 17, 2019 11:18
-
-
Save iso2022jp/749c27293fe3771f00fede15765d2e41 to your computer and use it in GitHub Desktop.
Make regex for date range (YYYY-MM-DD to YYYY-MM-DD)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const makeDateRangeRegexp = (from, to) => { | |
const indices = Array.from('01235689') | |
const unders = indices.map(i => { | |
const digit = from[i] | |
return digit > 0 ? from.slice(0, i) + `[0-${digit - 1}]` : null | |
}) | |
const overs = indices.map(i => { | |
const digit = to[i] | |
return digit < 9 ? to.slice(0, i) + `[${+digit + 1}-9]` : null | |
}) | |
const pattern = unders.concat(overs) | |
.filter(p => p) | |
.map(p => `(?!${p})`) | |
.join('') | |
.replace(/\[(\d)-\1]/g, '$1') | |
return new RegExp('^' + pattern) | |
} |
Author
iso2022jp
commented
Apr 24, 2018
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment