that modifier s at the end made . matches everything including \n;
as now . matches everything, we need to make it non-greedy;
make the entire string matched, so that the entire string will be substituted (in your original code, only the part before first \n is matched, and then that part is substituted with itself, so nothing is done);
For the part after the first \n, we match it, but not capturing it, so used ?: