Search code examples
javascriptregextokenize

Regexp in the Case of Single and Double Quotes


I have the following JavaScript:

let strTest = `
  "The issue": "L'oggetto ",
  "issue": "oggetto",
  "issue": 'oggetto "novo" ',
  `;

I'm trying to tokenize a string like the one above.

My regexp attempt:

let regExp = /["'](.*?)["']\s*?:\s*?['"](.*?)["']/gm;

This works fine, except in the case where I have a pair of single quotes (') inside of double quotes (") or vice-versa.

Is this possible with only one regular expression?


Solution

  • I answer my self , I think I came with a smaller regex:

    ` /["'](.*)["']\s*?:\s*?["'[](.*)["']]/g ` 
    

    Have a look at regex101.com/r/g9WCbi/1