0

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 class="test">9/2/2021 10:59:15 AM</td>
</tr>

<tr>
<td class="test">12/15/2015 12:10:45 PM</td>
</tr>

<tr>
<td class="test">10/10/2012 5:00:10 AM</td>
</tr>
</table>
user3683976
  • 121
  • 1
  • 7
  • You are returning the replacement on `$(document).ready` but if you want to update a certain element value with the replacement you should set it to that instead of returning. – The fourth bird Feb 02 '22 at 22:56
  • @Thefourthbird Thank you for the reply. Not sure what you mean updating a certain element value with the replacement – user3683976 Feb 02 '22 at 23:00
  • Your replacement works, and is in the variable `newText` What do you want to do with the value of that variable? – The fourth bird Feb 02 '22 at 23:01
  • what I'm wanting to do with is return the replaced regex and have it reflect on the browser – user3683976 Feb 02 '22 at 23:06
  • What I mean is that you should set the replacement to where you want to see it, an element value, the body of the document perhaps. Or get just the table `` 's that you want first and then replace those values. You can check out the posted pattern of sln to keep the existing `` as right now they will disapear as they are matched and not part of the replacement. – The fourth bird Feb 02 '22 at 23:23

2 Answers2

1

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

sln
  • 2,071
  • 1
  • 3
  • 11
  • I'm needing assistance with the jquery to reflect my regex change on the browser so it displays the change – user3683976 Feb 02 '22 at 23:07
  • yes I need a regex to parse the HTML :) – user3683976 Feb 02 '22 at 23:17
  • @user3683976 Then you should specifically say that and exclude any dom functional interface. I was downvoted by somebody who doesn't know the difference and who has it brainwashed the opinion that html can't be parsed with regex. I'm here to tell you I do it all the time. – sln Feb 02 '22 at 23:18
  • How would you then parse the HTML with jquery? – user3683976 Feb 02 '22 at 23:35
1

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');
  });
});
Andreas Louv
  • 46,145
  • 13
  • 104
  • 123
  • Thank you for the reply. Is there a way to replace it with ([0-3]?[0-9])\/([0-3]?[0-9])\/((?:[0-9]{2})?[0-9]{2}).*?<\/td> instead of using .test. I'm wanting it to completely remove the time stamp but keep the date in period format – user3683976 Feb 02 '22 at 23:16
  • 1
    You cannot parse HTML with regex see https://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454 – Andreas Louv Feb 02 '22 at 23:20
  • Thank you for the explanation Andreas! Didn’t realize that you can’t parse html like that but this will do – user3683976 Feb 02 '22 at 23:26