RegEx: Grabbing values between quotation marks

前端 未结 20 1274
暖寄归人
暖寄归人 2020-11-22 02:13

I have a value like this:

\"Foo Bar\" \"Another Value\" something else

What regex will return the

20条回答
  •  青春惊慌失措
    2020-11-22 02:43

    I've been using the following with great success:

    (["'])(?:(?=(\\?))\2.)*?\1
    

    It supports nested quotes as well.

    For those who want a deeper explanation of how this works, here's an explanation from user ephemient:

    ([""']) match a quote; ((?=(\\?))\2.) if backslash exists, gobble it, and whether or not that happens, match a character; *? match many times (non-greedily, as to not eat the closing quote); \1 match the same quote that was use for opening.

提交回复
热议问题