regex match text in either single or double quote

前端 未结 3 410
自闭症患者
自闭症患者 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

    You want to make sure the quote symbols are properly matched, so a quote starting with a single quote ends with a single quote. Also, the regex should allow for escaping a quote symbol with a backslash if it's the same symbol (double or single quote symbol) bounding the string. Try this:

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

    These samples match this regex:

    'sing"le q\'uote'

    "dou\"ble 'quote"

提交回复
热议问题