简体   繁体   中英

regex match uneascaped quoted string

I am string to match quoted strings in a sentence with regex, matching strings closed with quotes and also string at end of sentence which have only opening quotes.

For example in the sentence concat("hello ", name, "how are , I will match "world" and "again you .

This is my regex which works well

reg = /"[^"]*"?/g
input = 'concat("hello ",  name, "how are';
[...input.matchAll(reg)].map(m => m[0])
/** output:
[ 
  '"hello "', 
  '"how are' 
]


 */

but it fails when there is a backslashed quote.
For example for the sentence:
concat("say \"hello\" to ", name, "and to I would like to match "say \"hello\" to" , and "and to , but thhis is what the regex matches:

reg = /"[^"]*"?/g
input = 'concat("say \\"hello\\" to ",  name, "and to'
[...input.matchAll(reg)].map(m => m[0])
/** output (not the one I am trying to get):
[ 
  '"say \\"', 
  '" to "', 
  '"and to' 
]
 */

I tried adding to regex (?<!\\) before every " , so it matches only quotes that are no backslashed, but still get wrong result:

reg = /(?<!\\)"[^(?<!\\)"]*(?<!\\)"?/g
input = 'concat("say \\"hello\\" to ",  name, "and to'
[...input.matchAll(reg)].map(m => m[0])
/** result:
[ 
  '"say ', 
  '",  name, "' 
]

*/

what would be the correct regex?

This should do what you're looking for:

 const reg = /"(?:\\"|[^"])*"?/g; const input = 'concat("say \\"hello\\" to ", name, "and to'; console.log([...input.matchAll(reg)].map(m => m[0]));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM