JavaScript: Search a date within a string
JavaScript validation with regular expression: Exercise-4 with Solution
Search Date in String
Write a JavaScript program to search a date within a string.
Sample string:
"Albert Einstein was born in Ulm, on 14/03/1879."Visual Presentation:
Sample Solution:
JavaScript Code:
function is_dateString(str)
{
regexp = /^(1[0-2]|0?[1-9])\/(3[01]|[12][0-9]|0?[1-9])\/(?:[0-9]{2})?[0-9]{2}$/;
if (regexp.test(str))
{
return true;
}
else
{
return false;
}
}
console.log(is_dateString("01/01/2015"));
console.log(is_dateString("01/22/2015"));
console.log(is_dateString("32/01/2015"));
Output:
true true false
Flowchart:
Live Demo:
See the Pen javascript-regexp-exercise-4 by w3resource (@w3resource) on CodePen.
Improve this sample solution and post your code through Disqus.
Previous: Write a pattern that matches e-mail addresses.
Next: Write a JavaScript program that works as a trim function (string) using regular expression.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics