I have a string that contains dates in the mm-yyyy; yyyy-mm; yyyy-mm-dd, etc. formats. The goal is to convert the month portion of the date substring into a roman numeric notation, e.g.
12-2018 ... 2018-12-28
to be converted into
XII-2018 ... 2018-XII-28
I use this function to convert conventional into roman digits:
// https://stackoverflow.com/questions/9083037/convert-a-number-into-a-roman-numeral-in-javascript
function romanize(num) {
var lookup = {M:1000,CM:900,D:500,CD:400,C:100,XC:90,L:50,XL:40,X:10,IX:9,V:5,IV:4,I:1},roman = '',i;
for ( i in lookup ) {
while ( num >= lookup[i] ) {
roman += i;
num -= lookup[i];
}
}
return roman;
}
The regex used to find the month substring is
var re = /(19|20\d{2})\b-(\d{2})/g;
First parenthesized match ($1) is the year 19YY or 20YY only. The second ($2) is the month substring. The problem is that I cannot pass $2 as a parameter, i.e.
string = string.replace(re, "$1-" + romanize($2));