I can't access the example from you. I don't understand what you mean by "causes a problem when N1 is empty": The regex in the replace statement is explicitely forcing to have at least one word character (\w) on the right side.