我想用''
而不是''
替换'
,因为它们已经转义了。
使用查找非常简单:
(?<!'')'
请参阅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.