JavaScript regular expression that matches parentheses

I'm working with a json stream that requires decoding to extract complete json parts. The stream is in the following format:

{"a":1, "b":2}{"c":2,"e":3, "x":"eff"}{"3":4}

The regex pattern {([^}]+)} is able to extract complete groups as shown below:

{"a":1, "b":2}` & `{"c":2,"e":3, "x":"eff"}

An issue I'm encountering is that the string data may contain { or } characters which are always enclosed within double quotes (").

For example:

{"a":1, "b":2}{"c":2,"e":3, "x":"ab{cd}efg"}

Is there a way to create a regex pattern that splits this into groups like so?

{"a":1, "b":2}` and `{"c":2,"e":3, "x":"ab{cd}efg"}`

DEMO

Answer №1

Here is a regex solution for the given question: {(?:"[^"]*"|[^{])+} But, if there is a possibility of having escaped quotes inside double quotes, the updated regex should be: {(?:"(?:\\.|[^"])*"|[^{])+}

Answer №2

({(("|')[^{]+\3:(?:("|').+\4|\d+),?\s?)+})

This regular expression is designed to match brackets, single and double quotes (ensuring they are paired), as well as the same characters on the other side of the colon. It also includes numbers that are not enclosed in quotes.

Check it out in action here

If you want a simpler version that does not include matching single and double quotes, you can use:

({("[^{]+":(?:".+"|\d+),?\s?)+})

View the simplified version here

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Changing the content of an HTML table using JQuery's Ajax functionality

I have a table in my JSP page that loads data from the server. My question is, how can I load a new list of data from the server (servlet) and change the content of the same row when I enter a number in an input text box and press enter? I am trying to a ...

Scrolling and hovering now triggers the fixed button to toggle class seamlessly

Currently, I am attempting to modify the class of a button on my website. The initial state of the button is wide, but as the user scrolls, it should shrink in size. When hovering over the shrunken button, it should expand back to its original width. Alt ...

Ensuring session data is updated when saving a mongoose model

I am facing a challenge with updating express session data in the mongoose save callback. Is there a way to access and modify the session data from within line 4 of the code? app.get('/', function(req, res){ var newModel = new Model(somedata); ...

Is there a way for me to set up a confirmation pop-up alert before hitting the submit

I am interested in implementing a pop-up alert that asks "Are you sure you want to delete this comment?" when a user clicks on a button. How can I achieve this? $('button').on('click', function(){ // Display an alert to the user c ...

Sending data to JavaScript in ExpressJS

I am facing a bit of confusion here; I am utilizing NodeJS to retrieve a JSON file and I want to pass the data to my web page so that JavaScript can utilize it. app.get('/test', function(req, res) { res.render('testPage', { ...

Saving the current language (i18n) in local storage using VueJS

I'm working on a Vue app that utilizes Vue Routers and the Vue $i18n plugin. Here's how I've structured my HTML: <div class="locale-changer"> <select v-model="this.$i18n.locale" class="btn"> ...

My Tailwind CSS toggle button disappears in dark mode, why is that happening?

<button aria-label="Toggle Dark Mode" type="button" className="lg:inline-flex lg:w-40 md:w-screen p-3 h-12 w-12 order-2 md:order-3" onClick={() => setTheme(theme === 'dark' ? &ap ...

Steps to design a code editor with autocomplete dropdown using Material UI technology

I am currently working on a unique use case for my app that involves implementing an editor-like textarea with Material UI Chip components (similar to tags in an autocomplete textbox) that generate expressions. The goal is to have an autocomplete dropdown ...

Tips for resolving rendering page issues in an express app

My application is a straightforward blog platform that showcases a schema for the title, entry, and date of each blog post. There is also an edit/delete feature that is currently under development. When attempting to use the edit/delete button on a selecte ...

Tips for spinning each individual particle around its own axis within a Particle system in THREE.js?

I'm currently working on a space scene where I'm using a particle system to create a group of asteroids. I'd like to rotate each asteroid on its own axis. Below is the code snippet I'm using to set up the particle system: var asteroidG ...

Is it possible to create a unique AJAX function for a button element within a table?

Using an ajax function, I am populating a table with data from a PHP file that returns JSON encoded data. Each row in the table includes data as well as three buttons for delete, edit, and save operations specific to that row. The issue arises when I atte ...

Adjust the size of the image to perfectly fit within the div without enlarging it

I have a variety of images with different dimensions, ranging from 50x100 to 4000x4000 pixels. My objective is to display these images in their original size or scale them down to fit within the browser window. However, I have encountered an issue where t ...

Improve the functionality of select[multiple] to allow for single-click modifications without the need for CMD/CTRL

I am attempting to modify the default functionality of a select element so that clicking once on its options changes their selected state. Essentially, I want to eliminate the confusing requirement of holding down shift/ctrl to select multiple options. I ...

Having problems with CakePHP validation in the view?

Welcome to my first post on this platform! I've been searching for a solution but haven't found anything that works for me yet. I'm relatively new to PHP and CakePHP. Yesterday, I managed to get everything set up without any issues and was ...

Issue with JSON/Java not refreshing innerHTML when onClick event occurs

I'm having an issue with my JavaScript code: <script type="text/javascript"> $(document).ready(function() { $('#domaincheckbutton').click(function() { var finalMessage = document.getElementById('finalMessage'); ...

Searching and selecting columns in React using Material-UI

Currently, I am using Material-UI data tables and have implemented a search functionality similar to this Codesandbox example, which only searches the Name/Food column. This is my existing code snippet: const [filterFn, setFilterFn] = useState({ fn: items ...

Can an electron application be transformed into a web-based application?

From my understanding: Electron enables a javascript/html/css application to utilize web technologies within a desktop environment. I have come across the fact that most web applications can be transformed into a desktop application using electron. My ...

tips for navigating through an AngularJS $resource instance

I am facing a frustrating issue that I need assistance with. The problem arises when I try to extract specific data from each element of the stock data fetched by my controller from Yahoo Stocks. Although the data is stored in $scope.stocks and can be disp ...

What is the best method for including parameters in OBJECT_URL when sharing a post on Facebook?

Regarding this line: FB.api('/me/namespace:read' + '?article=http://www.xxxxxxxxxxxxxx/tm/redir.php&access_token=','post', Whenever I attempt: I encounter errors. How can I successfully pass parameters in the OBJECT_UR ...

Deactivate a Button until Another One is Clicked in React

Is there a way to disable the submit button until a rating has been provided? My Current State this.state = { stars: [ { active: false }, { active: false }, { active: false }, { active: false }, { active: fal ...