转义除标签属性之外的匹配引号
我想转义除标签属性中的匹配引号之外的匹配引号,例如:
输入:
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'
您能给我建议如何跳过整个不需要的块(“blah blah blah”)而不是只跳过第一个引用吗?
I want to escape matching quotes except those in tag properties, for example:
input:
xyz <test foo='123 abc' bar="def 456"> f00 'escape me' b4r "me too" but not this </tEsT> blah 'escape " me'
expected output:
xyz <test foo='123 abc' bar="def 456"> f00 \'escape me\' b4r \"me too\" but not this </tEsT> blah \'escape " me\'
I have following regexp:
$result = preg_replace('/(([\'"])((\\\2|.)*?)\2)/', "\\\\$2$3\\\\$2", $input);
which returns:
xyz <test foo=\'123 abc\' bar=\"def 456\"> f00 \'escape me\' b4r \"me too\" but not this </tEsT> blah \'escape " me\'
now I would like to use regexp zero-width negative look behind to skip matching quotes that have equal sign in front:
$result = preg_replace('/((?<=[^=])([\'"])((\\\2|.)*?)\2)/', "\\\\$2$3\\\\$2", $input);
but the result is still not as expected:
xyz <test foo='123 abc\' bar="def 456"> f00 \'escape me\' b4r "me too" but not this </tEsT> blah \'escape " me'
Can you please give me advice how I can skip whole unwanted block (="blah blah blah") instead of skipping just first quote?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不要向后看来建立背景,而要向前看。通常会容易得多。
我假设属性值中不会有任何尖括号。
Instead of looking backward to establish the context, look forward. It's usually much easier.
I'm assuming there won't be any angle brackets in the attribute values.
这是另一张。
Here's another one.