I have a jQuery with a regex that replaces the dates that have slashes with periods and removes the time stamp but keeps the date, but it is not reflecting on the web browser. How do I dynamically change the date and reflect the results on the browser? Any help is welcomed :)
$(document).ready(function ()
{
var oldText = $(this).text();
var newText = oldText.replace(new RegExp("<td.*?>([0-3]?[0-9])\/([0-3]?[0-9])\/((?:[0-9]{2})?[0-9]{2}).*?<\/td>","g"), "$1.$2.$3");
});
<table>
<tr>
<td >9/2/2021 10:59:15 AM</td>
</tr>
<tr>
<td >12/15/2015 12:10:45 PM</td>
</tr>
<tr>
<td >10/10/2012 5:00:10 AM</td>
</tr>
</table>
CodePudding user response:
Instead of trying to parse HTML with regex, then you should query for the relevant HTML elements, and update their texts:
$(document).ready(function() {
$('.test').text(function(index, oldText) {
return oldText.replace(/(\d )\/(\d )\/(\d ).*/, '$1.$2.$3');
});
});
CodePudding user response:
Your best bet is to replace with the stuff in the begging and end as well.
(<td.*?>)([0-3]?[0-9])\/([0-3]?[0-9])\/((?:[0-9]{2})?[0-9]{2})(.*?<\/td>)
replace $1$2.$3.$4$5
https://regex101.com/r/zrE2eh/1
You can use a variable look behind and ahead as well if you need to keep it to
the 1/2/3 capture groups as well.
(?<=<td.*?>)([0-3]?[0-9])\/([0-3]?[0-9])\/((?:[0-9]{2})?[0-9]{2})(?=.*?<\/td>)
Replace $1.$2.$3
https://regex101.com/r/OOJP7t/1
