regex match text in either single or double quote

前端 未结 3 403
自闭症患者
自闭症患者 2020-12-10 14:13

I want to match strings like:

The sentence is \'He said \"Hello there\"\'
The sentence is \"He said \'Hello there\'\"

and get back a single

3条回答
  •  独厮守ぢ
    2020-12-10 14:43

    Very sad, but such an elegant and accurate way does not work:

    (["'])(?:\\\1|[^\1]+)*\1
    

    But we can change it a little bit, and all works fine:

    (["'])((?:\\\1|(?:(?!\1)).)*)(\1)
    

    https://regex101.com/r/dKdBMT/2

    I would like to make sure that this regexp will work in all cases: please more test it.

提交回复
热议问题