How would a regex be to get only numbers and / in a string.
Example: Start date: 08/29/2016
Results: 08/29/2016
How would a regex be to get only numbers and / in a string.
Example: Start date: 08/29/2016
Results: 08/29/2016
According to this question in Stack Overflow , the suggested simple way is:
/^(0?[1-9]|[12][0-9]|3[01])[\/\-](0?[1-9]|1[012])[\/\-]\d{4}$/
But this expression like ( reported in a comment ) of response is not correct for February 29 ,
Another user suggested the following expression:
var reg = /^(((0[1-9]|[12]\d|3[01])\/(0[13578]|1[02])\/((19|[2-9]\d)\d{2}))|((0[1-9]|[12]\d|30)\/(0[13456789]|1[012])\/((19|[2-9]\d)\d{2}))|((0[1-9]|1\d|2[0-8])\/02\/((19|[2-9]\d)\d{2}))|(29\/02\/((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))))$/g;
Although it's much more complex than the one I've been using, I've never encountered any errors in it.
In the same question you will find ways to validate the date using functions rather than using regex, if it is more interesting to you, why not?
You can do this:
var data1 = "Data de inicio: 29/08/2016";
var data2 = data1.replace(/[^0-9/]/g, '');
document.write(data2);