regEx to match all double quotes wrapped in brackets

56 Views Asked by At

Looking for some help on this one. I need to match all double quotes between {} brackets. Then I will escape these double quotes.

(37, "2012 Fall", null, null, 0, 1, "1420", {"canDelete":false, "cantDeleteModes":[2, 3, 5]}, "2020-05-28T18:06:48.000Z", "2020-10-27T19:42:03.000Z", 1, 1);

Here is the reqex I have so far...

/(?<=\{).*?(?=\})/g

but that matches everything between the {} brackets.

Expected output...

(37, "2012 Fall", null, null, 0, 1, "1420", {\"canDelete\":false, \"cantDeleteModes\":[2, 3, 5]}, "2020-05-28T18:06:48.000Z", "2020-10-27T19:42:03.000Z", 1, 1);

Any help would be appreciated ;=)

3

There are 3 best solutions below

1
Martin del Necesario On BEST ANSWER

This code should get you going:

const input = `(37, "2012 Fall", null, null, 0, 1, "1420", {"canDelete":false, "cantDeleteModes":[2, 3, 5]}, "2020-05-28T18:06:48.000Z", "2020-10-27T19:42:03.000Z", 1, 1);`

const regex = /{(.*)}/

const substr = input.match(regex)[1]
const replacement = substr.replaceAll('"', '\\"')

const result = input.replace(substr, replacement)

console.log(result)

Explanation

  • /{(.*)}/ matches everything between brackets {} non-lazily
  • input.match(regex)[1] - returns the string of the first match

Remarks:
It was the easiest solution for me to write and to understand. If you need something more performant, one possible way of going about it would be to iterate over the chars of the string, be aware of any {} brackets and replace the quotes only when an opening bracket but no closing bracket was seen before (and keep track of multiple opening and closing brackets so to note the beginning. So you'd need some kind of "level"-counter which increases and decreases depending on the occurrence of opening and closing brackets).

0
mplungjan On

If you want to match canDelete and cantDeleteModes you can do better than regex

const str = `(37, "2012 Fall", null, null, 0, 1, "1420", {"canDelete":false, "cantDeleteModes":[2, 3, 5]}, "2020-05-28T18:06:48.000Z", "2020-10-27T19:42:03.000Z", 1, 1)`
const arr = JSON.parse(`[${str.slice(1,-1)}]`)
console.log(arr)
console.log(Object.entries(arr[7])); // use Object.keys to just get the key or Object.values to just get the values

0
Alexander Nenashev On

Parsing JSON should be done with JSON.parse but regarding regexps you could use regexp assertions and RegExp::exec to incrementally search strings. Notice that we include the quotes in the match since we want to skip them in the next exec. The extra regexp group help us to get the result without the quotes.

But the solution is brittle since if you put the curly brackets inside your string it could fail. If your source string doesn't comply JSON then the better option is to write your own parser.

const str = '(37, "2012 Fall", null, null, 0, 1, "1420", {"canDelete":false, "cantDeleteModes":[2, 3, 5]}, "2020-05-28T18:06:48.000Z", "2020-10-27T19:42:03.000Z", 1, 1, {"cantDelete":false, "cantDeleteModes":[2, 3, 5]});';

const regexp = new RegExp('(?<=\{[^}]*)"([^"]+)"', 'g');

const matches = [];
let match;
while(match = regexp.exec(str)){
  matches.push(match[1]);
}

console.log(matches);