[JENKINS-14583] Move replacement into shared function for reuse