Home > Mobile >  How do I remove curly braces from JSON strings using regular expression?
How do I remove curly braces from JSON strings using regular expression?

Time:05-06

Hi I have this JSON file I want to remove the curly braces from inside the strings in the chapters array, but if this curly braces contain more than 3 words (2 spaces inside the curly braces), I want the complete curly brace to be deleted. Is it possible with Regular expression?

 {
    "abbrev": "gn",
    "name": "Genesis",
    "chapters": [
        [
            "And the earth was without form, and void; and darkness {was} upon the face of the deep. And the Spirit of God moved upon the face of the waters.",
            "And God said, Let there be light: and there was light.",
            "And God saw the light, that {it was} good: and God divided the light from the darkness. {the light from...: Heb. between the light and between the darkness}",
            "And God called the light Day, and the darkness he called Night. And the evening and the morning were the first day. {And the evening...: Heb. And the evening was, and the morning was etc.}",
            "And God said, Let there be a firmament in the midst of the waters, and let it divide the waters from the waters. {firmament: Heb. expansion}"
        ]
 ] }

I tried solving with negative look ahead, unsatisfactory results. And if its not possible with Regular expression what would be the solution for it in javascript?

CodePudding user response:

I don't see why you couldn't solve this using 2 regexes:

var regex1 = /{[^{}] [ ][^{}] [ ][^{}] }/g;
var regex2 = /[{}]/g;

var str = ...;
str = str.replace(regex1, "");
str = str.replace(regex2, "");
  • Related