How can I match a quote-delimited string with a regex?

前端 未结 9 724
一生所求
一生所求 2020-12-01 05:05

If I\'m trying to match a quote-delimited string with a regex, which of the following is \"better\" (where \"better\" means both more efficient and less likely to do somethi

9条回答
  •  温柔的废话
    2020-12-01 05:42

    More complicated, but it handles escaped quotes and also escaped backslashes (escaped backslashes followed by a quote is not a problem)

    /(["'])((\\{2})*|(.*?[^\\](\\{2})*))\1/
    

    Examples:
      "hello\"world" matches "hello\"world"
      "hello\\"world" matches "hello\\"

提交回复
热议问题