:
var months = ['Sau', 'Vas', 'Kov', 'Bal', 'Geg', 'Bir',
'Lie', 'Rgp', 'Rgs', 'Spa', 'Lap', 'Grd']
function transformMonth (str, toText) {
str = str.split('/')
str[1] = toText ? months[str[1] - 1] : 1 + months.indexOf(str[i])
return str
}
console.log(transformMonth('2015/Sau/01', false))
console.log(transformMonth('2015/01/01', true))
, :
/
(\d+)
\/
(\w+)
\/
(\d+)
/
Then all you have to do is return the first capture group (year) plus (1 + months.indexOf(month)(month) plus the third capture group (day).
var months = ['Sau', 'Vas', 'Kov', 'Bal', 'Geg', 'Bir',
'Lie', 'Rgp', 'Rgs', 'Spa', 'Lap', 'Grd']
function toNumberMonth (str) {
return str.replace(/(\d+)\/(\w+)\/(\d+)/, function (date, year, month, day) {
return year + '/' + (1 + months.indexOf(month)) + '/' + day
})
}
Returning is a matter of setting up a regular expression for use (\d+)in all three capture groups:
function toTextMonth (str) {
return str.replace(/(\d+)\/(\d+)\/(\d+)/, function (date, year, month, day) {
return year + '/' + months[month-1] + '/' + day
})
}