Home > Blockchain >  Regex expression to separate time and replace it with separators
Regex expression to separate time and replace it with separators

Time:10-28

I need a regex to separate a time string written this way: 2019 08 24 - 154315631.jpg.

The last 9 digits (before the extension) are the time. First I need to remove the last 3 digits and then I need to separate like this: 2019 08 24 - 15:43:15.jpg

CodePudding user response:

We can grab everything you need into 4 groups and skip the last 3 digits before an extension

$1 - (.*?-\s\d\d) - 2019 08 24 - 15

$2 - (\d\d) - 43

$3 - (\d\d) - 15

$4 - (.*) - .jpg

and replace the matched string with the contents of the groups, separating the groups $1, $2 and $3 with a colon

"2019 08 24 - 154315631.jpg".replace(/(.*?-\s\d\d)(\d\d)(\d\d)\d*(.*)/, "$1:$2:$3$4");
  • Related