JavaScript structural search and replace