Regex用于转义尚未转义的单引号


Regex for escaping single quotes which are not already escaped

我想用''而不是''替换',因为它们已经转义了。

使用查找非常简单

(?<!'')'

请参阅regex101.com上的演示。
对于PHP:,您还需要转义反斜杠

<?php
$string = "I want to replace ' with '' but not '' since they are already escaped";
$regex = "~(?<!''')'~";
echo preg_replace($regex, "'''", $string);
# Output: I want to replace '' with '' but not '' since they are already escaped
?>

请参阅ideone.com.

上的演示