Reputation: 2048
I have a function to check a part of my html page for structure. I basically want to identify if div elements start with a date in the text/content or not. if so, then I would like to alter the HTML (but that is out of scope for this question.
My control is as followed:
function isDate(_date){
const _regExp = new RegExp('^(-?(?:[1-9][0-9]*)?[0-9]{4})-(1[0-2]|0[1-9])-(3[01]|0[1-9]|[12][0-9]) (2[0-3]|[01][0-9]):([0-5][0-9])?$');
return _regExp.test(_date);
}
$(function() {
var entries = $("div.histEntry");
console.log('2018-08-01 18:30 is Date? ' + isDate('2018-08-01 18:30'));
console.log('hungry like a wolf is Date? ' + isDate('hungry like a wolf'));
$("div.histEntry").each(function( index, value ) {
console.log('content of div: ' + $(this).text());
console.log('text to check: ' + $(value).text().substring(0, 17));
console.log('is Date?: ' + isDate($(value).text().substring(0, 17)));
});
});
Here is a snippet from what I get returned in the console:
2018-08-01 18:30 is Date? true
hungry like a wolf is Date? false
content of div:
2019-04-12 13:55 John Doe/Web/ACME created this document
text to check:
2019-04-12 13:55
is Date?: false
content of div:
2019-04-15 14:03 Office Manager changed from Jim Joe to Billy Boy
text to check:
2019-04-15 14:03
is Date?: false
content of div:
Mikael commented this document
text to check:
Mikael commented
is Date?: false
Why is 2018-08-01 18:30 seen as date and 2019-04-15 14:03 not?
Upvotes: 0
Views: 27
Reputation: 606
Maybe you are testing a string that includes a white space at the end. Have you tried to check $(value).text().substring(0, 16)
?. Just in case it's that simple.
Upvotes: 2