Skip to content

Instantly share code, notes, and snippets.

@memochou1993
Last active June 17, 2024 08:07
Show Gist options
  • Save memochou1993/f94a1059292244ce69015d969418c0dc to your computer and use it in GitHub Desktop.
Save memochou1993/f94a1059292244ce69015d969418c0dc to your computer and use it in GitHub Desktop.
function validateCronExpression(input) {
const segment = '(\\*|(T|\\*\\/[1-9][0-9]*|(T)(-(T))?)(,(T|\\*\\/[1-9][0-9]*|(T)(-(T))?))*)';
const minute = segment.replaceAll('T', '[0-5]?[0-9]');
const hour = segment.replaceAll('T', '[0-1]?[0-9]|2[0-3]');
const dayOfMonth = segment.replaceAll('T', '[1-9]|[12][0-9]|3[01]');
const month = segment.replaceAll('T', '[1-9]|1[0-2]');
const dayOfWeek = segment.replaceAll('T', '[0-6]');
const cronRegex = new RegExp(`^${minute}\\s+${hour}\\s+${dayOfMonth}\\s+${month}\\s+${dayOfWeek}$`);
return cronRegex.test(input);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment