我想逃避匹配报价,除了那些在标签属性,例如:除了标签的属性逃逸匹配报价
输入:
xyz <test foo='123 abc' bar="def 456"> f00 'escape me' b4r "me too" but not this </tEsT> blah 'escape " me'
预期输出:
xyz <test foo='123 abc' bar="def 456"> f00 \'escape me\' b4r \"me too\" but not this </tEsT> blah \'escape " me\'
我有以下正则表达式:
$result = preg_replace('/(([\'"])((\\\2|.)*?)\2)/', "\\\\$2$3\\\\$2", $input);
返回:
xyz <test foo=\'123 abc\' bar=\"def 456\"> f00 \'escape me\' b4r \"me too\" but not this </tEsT> blah \'escape " me\'
现在我想用正则表达式零宽度负的外观后面跳过有等号前面匹配的引号:
$result = preg_replace('/((?<=[^=])([\'"])((\\\2|.)*?)\2)/', "\\\\$2$3\\\\$2", $input);
但结果仍不如预期:
xyz <test foo='123 abc\' bar="def 456"> f00 \'escape me\' b4r "me too" but not this </tEsT> blah \'escape " me'
能否请您给我的意见,我怎么可以跳过整个不必要的块(=“等等等等等等”),而不是仅仅跳过第一个报价?
不要用正则表达式来做到这一点。你会后悔的。 – Jon 2012-02-17 10:40:11