我想逃避匹配報價,除了那些在標籤屬性,例如:除了標籤的屬性逃逸匹配報價
輸入:
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