0

I'm trying to create a JSON file which will hold a regular expression. for example:

    {
    "FrameworksData": [{
            "name": "jquery",
            "regexes": ["script\s+src\s*=\s*\"jquery", "b", "c"],
            "extensions": [".js"],
            "conditions":"t1=r1||r2||r3; t2=t1&&e1; res=t2;"            
        }
    ]
}

I need to use the double/single quotes as part of the regex that is located inside the JSON. However this JSON is not valid as a result of the structure \"jquery. how do I use the single quotes and the double quotes in the JSON so I can achieve the correct regex? Thanks!

mary
  • 869
  • 5
  • 13
  • 26
  • 1
    Let's take a step back. Who/what is generating the above JSON? Ideally, the escaping should already be handled for you. Most likely, content double quotes would just be escaped with a single backslash (I think). – Tim Biegeleisen Jun 12 '18 at 06:52
  • I'm creating this JSON manually. – mary Jun 12 '18 at 06:53

1 Answers1

1

Multiple techniques can be adopted:

  1. Escape the backslash \ i.e to \\. For scripted escaping you can use this solution

"FrameworksData": [{"regexes": ["script\\s+src\\s*=\\s*\"jquery", "b", "c"]}]

  1. You can use encodeURIComponent to transfer the regexes if you don't want to escape.
Avezan
  • 673
  • 4
  • 9