<?php
$string = "I want to replace ' with \' but not \' since they are already escaped";
$regex = "~(?<!\\\)'~";
PD9waHAKJHN0cmluZyA9ICJJIHdhbnQgdG8gcmVwbGFjZSAnIHdpdGggXCcgYnV0IG5vdCBcJyBzaW5jZSB0aGV5IGFyZSBhbHJlYWR5IGVzY2FwZWQiOwokcmVnZXggPSAifig/PCFcXFwpJ34iOwoKZWNobyBwcmVnX3JlcGxhY2UoJHJlZ2V4LCAiXFwnIiwgJHN0cmluZyk7Cg==