A1. The dogs, which barked in the night-time, did not recognize the thief.
A2. The dogs which barked in the night-time did not recognize the thief.
B1. The dogs, that barked in the night-time, did not recognize the thief.
B2. The dogs that barked in the night-time did not recognize the thief.
As far as I know, both A1 and A2 are "standard" English and the their meaning, as one can notice, is rather different.
Could one replace which with that in both A1 and A2 cases without infringing the rules of grammar? Could the absence or the presence of punctuation (please, note the parenthetical commas) interfere with or impede in any way this replacement?