I am not sure if you can do it with just one regex and you will have to probably do it as a two step process. First, you can capture the three capital letters using substring()
method and then you can replace all slashes with those three letter appearing in the beginning of character after first dot. Here is a demo with JS code,
function transformLine(s) {
var repStr = s.substring(1,4);
var replacedStr = s.replace(/\//g, repStr);
return replacedStr.substring(1,replacedStr.length);
}
var lines = [".PMC.89569XX/90051XX/90204XX/89533XX/90554XX/90053XX/90215XX/89874XX/89974XX/90481XX/90221XX/90508XX/90183XX/88526XX/89843XX/88041XX/90446XX/88515XX/89574XX/89847XX/88616XX/90513XX/90015XX/90334XX/89649XX.T00", ".PAJ.77998XX/77896XX.T00", ".PAG.78116XX/78104XX/77682XX/07616XX/77663XX/77863XX/07634XX/78088XX/77746XX/78148XX.T00", ".PKC.22762XX/22358XX/22055XX/22672XX/22684XX/22154XX/22608XX/22768XX/22632XX/22266XX/22714XX/22658XX/22631XX/22288XX/22020XX/22735XX/22269XX/22138XX/22331XX/22387XX/22070XX/22636XX/22629XX/22487XX/22725XX.T00"];
for (var i = 0;i<lines.length;i++) {
console.log("Before: " + lines[i]);
console.log("After: " + transformLine(lines[i])+"\n\n");
}
I've replaced the first dot as your expected output does not have it.
Let me know if this works for you.
Edit:
I have updated the code to provide a function that takes a string as input and returns the modified string. Please check the demo.
Edit2: Solving it mostly using regex
This one liner in the function does all the job for you in transforming your line to the required one.
function transformLine(s) {
return s.replace(/\//g, /^.(.{3})/.exec(s)[1]).replace(/^./,'');
}
var lines = [".PMC.89569XX/90051XX/90204XX/89533XX/90554XX/90053XX/90215XX/89874XX/89974XX/90481XX/90221XX/90508XX/90183XX/88526XX/89843XX/88041XX/90446XX/88515XX/89574XX/89847XX/88616XX/90513XX/90015XX/90334XX/89649XX.T00", ".PAJ.77998XX/77896XX.T00", ".PAG.78116XX/78104XX/77682XX/07616XX/77663XX/77863XX/07634XX/78088XX/77746XX/78148XX.T00", ".PKC.22762XX/22358XX/22055XX/22672XX/22684XX/22154XX/22608XX/22768XX/22632XX/22266XX/22714XX/22658XX/22631XX/22288XX/22020XX/22735XX/22269XX/22138XX/22331XX/22387XX/22070XX/22636XX/22629XX/22487XX/22725XX.T00"];
for (var i = 0;i<lines.length;i++) {
console.log("Before: " + lines[i]);
console.log("After: " + transformLine(lines[i])+"\n\n");
}
As you can see here, this line,
return s.replace(/\//g, /^.(.{3})/.exec(s)[1]).replace(/^./,'');
does all the job you need. It first extracts the three capital letter using this /^.(.{3})/.exec(s)[1]
then all slashes are replaced with this captured word and then finally first character which is a dot is removed using this /^./,''
and finally returns the string you need.
Let me know if this is what you wanted. Else let me know if you further wanted it in any particular way.