search

Home  >  Q&A  >  body text

Regular expression to get string between single or double quotes even if it is empty

Here's the regex I'm trying:

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

This is the text I gave

val1=""val2>"2022-11-16 10:19:20"

I also need a blank expression like val1, i.e. I need something like the following in my contest

""
2022-11-16 10:19:20

If I change the text to look like below I will get the correct output

val2>"2022-11-16 10:19:20"val1=""

Can anyone tell me what's wrong with me

P粉554842091P粉554842091237 days ago427

reply all(1)I'll reply

  • P粉064448449

    P粉0644484492024-04-02 15:32:25

    Use alternatives to match both cases.

    One alternative matches a pair of quotes, another uses lookahead to match the inside of two quotes.

    ""|(?<=")[^"]+(?=")

    reply
    0
  • Cancelreply