写一个 regex 来 match 一个完整的字符串 literal

2015-06-09 17:05:05 +08:00
 alphonsez

双引号里面可能有\"这种escapsed quote,以及其他escapse的字符,比如\n\啥的。
stackoverflow搜了这个:/"(?:[^"\\]|\\.)*"/

大多数情况可以work了,大家觉得还有啥bug吗?

1766 次点击
所在节点    正则表达式
1 条回复
alphonsez
2015-06-09 17:25:59 +08:00
试了一下:
```
"Foo \"Bar\"", "\"^(background|background\-attachment|background\-color|background\-image|background\-position|background\-position\-x|background\-position\-y|background\-repeat|border|border\-bottom|border\-bottom\-color|border\-bottom\-style|border\-bottom\-width|border\-collapse|border\-color|border\-left|border\-left\-color|border\-left\-style|border\-left\-width|border\-right|border\-right\-color|border\-right\-style|border\-right\-width|border\-spacing|border\-style|border\-top|border\-top\-color|border\-top\-style|border\-top\-width|border\-width|bottom|clear|clip|color|cursor|direction|display|filter|float|font|font\-family|font\-size|font\-size\-adjust|font\-stretch|font\-style|font\-variant|font\-weight|height|left|letter\-spacing|line\-break|line\-height|list\-style|list\-style\-image|list\-style\-position|list\-style\-type|margin|margin\-bottom|margin\-left|margin\-right|margin\-top|marker\-offset|max\-height|max\-width|min\-height|min\-width|orphans|outline|outline\-color|outline\-style|outline\-width|overflow|overflow\-X|overflow\-Y|padding|padding\-bottom|padding\-left|padding\-right|padding\-top|position|right|size|table\-layout|text\-align|text\-align\-last|text\-decoration|text\-indent|text\-justify|text\-overflow|text\-shadow|text\-transform|text\-autospace|text\-kashida\-space|text\-underline\-position|top|vertical\-align|visibility|white\-space|widows|width|word\-break|word\-spacing|word\-wrap|writing\-mode|z\-index|zoom)$\"", "Foo \"Bar\""
```
发现上面的直接crash了。估计回朔太多。我搞了个Patterns应用来测的。

同一个stackoverflow页面上的这个能很好work:
```/"([^"\\]*(\\.[^"\\]*)*)"/```

有知道为什么的吗?

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/197264

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX