Is it possible to convert a string of elements in JavaScript into JSON format?

Within my JavaScript code, a variable holds the following information:

url=http://localhost
quality=100
tag="4.4, 5.5"

I am interested in converting this data to JSON format using JavaScript, like this:

"result": {
    "url": "http://localhost",
    "quality": "100",
    "tag": "4.4, 5.5",
}

What is the best way to accomplish this conversion to JSON?


HTML

<textarea cols="100" rows="10" id="textarea_one"></textarea>
<textarea cols="100" rows="10" id="textarea_two"></textarea>

JS

var objLoadHTML = document.getElementById('textarea_one');
var strContent = objLoadHTML.value;


var Reg = /(?:(\w+)=([^\n\r]+))*/gm;
var match = Reg.exec(strContent);

while (match != null) {
    document.getElementById('textarea_two').innerHTML += match;
    match = Reg.exec(strContent);
}


document.getElementById('textarea_two').innerHTML = match;

Answer №1

To extract keys and values from your example, you can utilize a regex pattern such as:

/(?:(\w+)=([^\n\r]+))*/gm

By applying this regex, you will be able to capture the desired information.

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

Developing with Phonegap Build: A Guided Process

With all the conflicting information available, I am seeking clarity on this topic. Objective: To create and enhance a Phonegap app using Phonegap Build. 1) My preference is to utilize Phonegap Build without needing to install Android and iOS SDKs. 2) I ...

Is it possible to display a WP $post object in JSON format that is compatible with the REST API directly from a theme file?

I am currently in the process of developing a website using Backbone.js that is integrated with WordPress as the content management system. To efficiently retrieve data, my Backbone app is set up to fetch JSON from the WP REST API. I am looking to preload ...

Error in accessing the value from the JSON response

After uploading a photo to an external cloud CDN, I receive a JSON response containing relevant information about the uploaded photo. One key piece of data is the public_id field, which I need to store in my database. The response structure is as follows: ...

The error occurred in Commands.ts for Cypress, stating that the argument '"login"' cannot be assigned to the parameter of type 'keyof Chainable<any>))`

Attempting to simplify repetitive actions by utilizing commands.ts, such as requesting email and password. However, upon trying to implement this, I encounter an error for the login (Argument of type '"login"' is not assignable to parameter of t ...

You need to provide a non-null String in order to display text in a Text widget

As a beginner in flutter development, I am currently working on creating a quiz app where each quiz consists of five questions. However, I have encountered some challenges: 1- I want to ensure that the same question does not repeat within the same quiz, a ...

When a div tag containing PHP is empty, it should either be hidden or show specific text based on your requirements

Among the plethora of unanswered queries regarding hiding empty divs, I find myself unable to make any of the suggested solutions work. Hence, I am putting forth my own question. On my webpage, there is a specific section dedicated to showcasing various it ...

Node.js: The REST client delivers the response even before it has been officially returned

I've been experimenting with the node-rest-client REST client in Node.js. However, I'm facing an issue where when I run the code snippet below, it returns a value of null. Oddly enough, the response is printed to the console after that. Is there ...

Exploring AngularJS: the power of directives and the art of dependency

According to Angular documentation, the recommended way to add a dependency is by following these steps: Source //inject directives and services. var app = angular.module('fileUpload', ['ngFileUpload']); app.controller('MyCtrl&ap ...

Learning the ins and outs of Node.js: Creating a client to connect to a Node.js server and receive broadcast messages

In implementing my nodeJS Server, everything seems to be running smoothly. However, now I am looking to create a client that can receive messages from the server and trigger specific JavaScript functions based on those messages. The process involves: Us ...

Trigger callback function when user selects a date on the calendar using Material UI X Date Picker

I am currently utilizing the DateTimePicker component in material ui, version 5. I am looking for a way to intercept the callback triggered when a user clicks on a day in the calendar selector: https://i.stack.imgur.com/0Tlogm.png Although the DateTimePi ...

Tips for utilizing useQuery when props change using Apollo:

I am currently facing a challenge with my BooksList component where I need to pass props down to the BooksDetails component only when a title is clicked. I am trying to figure out how to utilize an Apollo hook to query only on prop changes. I have been ex ...

Unexpected token { in Fuse-Box when using Typescript

Here's the beginning of my fuse.ts file import { CSSPluginOptions } from 'fuse-box/plugins/stylesheet/CSSplugin'; import { argv } from 'yargs'; import * as path from 'path'; import { CSSPlugin, CSSResourcePlugin, Env ...

The expression `Object.prototype.toString.call(currentFruit) === "[object Date]"` checks if the object referenced by `current

Hi, I'm just starting to learn JavaScript and I have a question about an if condition that I came across in my code. Can someone please explain what this specific if condition does? Object.prototype.toString.call(currentFruit) === "[object Date]& ...

Discovering the specific DOM element that has been clicked using only JavaScript

Looking to enhance my HTML document with interactivity, I wanted to achieve a feature where clicking on a div element would display its respective id. I attempted the following approach: window.onload = function() { associateIds(); clicked(); } fu ...

Utilizing Angular's ng-Grid with Promises

My current setup involves fetching a JSON file through a service to serve as the data source for my grid. The service successfully fetches the data, and the grid renders its basic layout correctly. However, there seems to be an issue with populating the gr ...

Having trouble establishing a connection between socket.io client and server

I am currently attempting to establish a connection between a client and server using express and node.js. Unfortunately, I am encountering difficulties in connecting to the server. The tutorial I followed (available at https://socket.io/get-started/chat) ...

The HTTP client is unable to retrieve JSON data from the web server

async function fetchData() { const URL = "http://8tracks.com/mix_sets/all.json?include=mixes?api_key=05570e44383665661d8edeeb5d4f07d415e14b4a"; const response = await fetch(URL); const data = await response.json() ...

Insert a line break element following every 12th character within a given string

$my_string = "Lorem Ipsum is simply dummy text of the printing and typesetting industry." Desired Output : "Lorem Ipsum <br/> is simply<br/> dummy text<br/>of the printing<br/> and typesetting<br/> industry." The tag should ...

AngularJS - the element of surprise in execution sequence

Encountering a puzzling issue that exclusively affects Internet Explorer (any version) and not Chrome. An "items" array is stored within the "doc" object. Users have the capability to edit items, which essentially deletes the item but retains its content ...

Choosing an element beneath a table row using a different element as a reference

I'm attempting to identify the checkboxes associated with the link containing delete.jpg. Here's what I've tried so far: <table> <tr class="odd"> <td><input id="cbox1" type="checkbox"></input></td> ...