Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

regular expression add double quotes around values and keys in javascript

i need a valid JSON format to request ES. i have a string like

{ 
time:  { 
          from:now-60d,
          mode:quick,
          to:now } 
}

but when i try to use JSON.parse i got error because my string should be like

 { 
time:  { 
          "from":"now-60d",
          "mode":"quick",
          "to":"now" } 
}

so my question, there is any solution to add double quotes around keys and values of my string ?

thanx

like image 825
Idriss Benbassou Avatar asked Jun 15 '17 08:06

Idriss Benbassou


People also ask

How do you add double quotes in regex?

Firstly, double quote character is nothing special in regex - it's just another character, so it doesn't need escaping from the perspective of regex. However, because Java uses double quotes to delimit String constants, if you want to create a string in Java with a double quote in it, you must escape them.

How do you write double quotes in JavaScript?

to show double quote you can simple use escape character("\") to show it.

How do you append quotes in JavaScript?

We can use the backslash ( \ ) escape character to prevent JavaScript from interpreting a quote as the end of the string. The syntax of \' will always be a single quote, and the syntax of \" will always be a double quote, without any fear of breaking the string.

How do you add a double quote to a string?

To place quotation marks in a string in your code In Visual Basic, insert two quotation marks in a row as an embedded quotation mark. In Visual C# and Visual C++, insert the escape sequence \" as an embedded quotation mark. For example, to create the preceding string, use the following code.

How to display string with double quotation mark in JavaScript?

See the below example, how it will be done: var ssq = 'It's an example of printing the single quote with string.'; It's an example of printing the single quote with string. In JavaScript, you can use &quot with a string to display string with double quotation mark. With &quot, you can use any quote.

How to add double quotes to a string in C++?

Suppose, if we want to add double quotes to a string then we need [ "] escape sequence to escape quotes. Let us suppose if we have a string "Preeti" and simply it will display like this preeti and if we want to add double quotes to string (i.e. display like "preeti" ) then we will write a statement like this ""Preeti"".

How to add double quotes to Oops in Java?

// For adding double quotes to OOPS // then we need or add " escape sequence to escape quotes // around both side string. String str2 = " " OOPS " "; System. out. println ("Display String without quotes " + " " + str1); System. out. println ("Display String with quotes " + " " + str2); } }

What do double quotes around a string mean in Perl?

Double quotes around a string are used to specify a regular expression search (compatible with Perl 5.005, using the Perl-compatible regular expressions library written by Philip Hazel). Regular expressions are a very powerful concept but rather hard to explain from scratch.


2 Answers

maybe you can use :

str.replace(/([a-zA-Z0-9-]+):([a-zA-Z0-9-]+)/g, "\"$1\":\"$2\"");

Here is

regex demo


Note

In the group [a-zA-Z0-9-] of characters i use alphabetical digits and a -, maybe you need other so you can use another one

like image 51
YCF_L Avatar answered Oct 10 '22 08:10

YCF_L


This function will add quotes and remove any extra commas at the end of objects

function normalizeJson(str){return str.replace(/"?([\w_\- ]+)"?\s*?:\s*?"?(.*?)"?\s*?([,}\]])/gsi, (str, index, item, end) => '"'+index.replace(/"/gsi, '').trim()+'":"'+item.replace(/"/gsi, '').trim()+'"'+end).replace(/,\s*?([}\]])/gsi, '$1');}

Edit:

This other function supports json arrays.

It also converts single quotes to double quotes, and it keeps quotes off of numbers and booleans.

function normalizeJson(str){
    return str.replace(/[\s\n\r\t]/gs, '').replace(/,([}\]])/gs, '$1')
    .replace(/([,{\[]|)(?:("|'|)([\w_\- ]+)\2:|)("|'|)(.*?)\4([,}\]])/gs, (str, start, q1, index, q2, item, end) => {
        item = item.replace(/"/gsi, '').trim();
        if(index){index = '"'+index.replace(/"/gsi, '').trim()+'"';}
        if(!item.match(/^[0-9]+(\.[0-9]+|)$/) && !['true','false'].includes(item)){item = '"'+item+'"';}
        if(index){return start+index+':'+item+end;}
        return start+item+end;
    });
}

I also tested the regex with the safe-regex npm module

like image 22
SwiftNinjaPro Avatar answered Oct 10 '22 07:10

SwiftNinjaPro