V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
alphonsez
V2EX  ›  正则表达式

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

  •  1
     
  •   alphonsez · 2015-06-09 17:05:05 +08:00 · 1753 次点击
    这是一个创建于 3242 天前的主题,其中的信息可能已经有所发展或是发生改变。

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

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

    1 条回复    2015-06-09 17:25:59 +08:00
    alphonsez
        1
    alphonsez  
    OP
       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:
    ```/"([^"\\]*(\\.[^"\\]*)*)"/```

    有知道为什么的吗?
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5398 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 07:27 · PVG 15:27 · LAX 00:27 · JFK 03:27
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.