如何将带引号的字符串与其中的转义引号匹配? [英] How to match a quoted string with escaped quotes in it?

查看:48
本文介绍了如何将带引号的字符串与其中的转义引号匹配?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

/^"((?:[^"]|\\.)*)"/

针对这个字符串:

"quote\_with\\escaped\"characters" more

它只匹配到 \",虽然我已经明确地将 \ 定义为转义字符(并且它匹配 \_\\ 很好...).

It only matches until the \", although I've clearly defined \ as an escape character (and it matches \_ and \\ fine...).

推荐答案

如果您颠倒两个选项的顺序,它可以正常工作:

It works correctly if you flip the order of your two alternatives:

/^"((?:\\.|[^"])*)"/

问题是,否则重要的 \ 字符会在 before 尝试匹配 \" 之前被吃掉.它之前对 有效\\\_ 只是因为任一对中的两个字符都与您的 [^"] 匹配.

The problem is that otherwise the important \ character gets eaten up before it tries matching \". It worked before for \\ and \_ only because both characters in either pair get matched by your [^"].

这篇关于如何将带引号的字符串与其中的转义引号匹配?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆