locked
Create a REGEX that Will Match Both Compound and Single JSON Key Value Pairs RRS feed

  • Question

  • User1928065626 posted

    Suppose I am trying to convert the following string to a JSON string by surrounding each key and value pair by single quotes.

    {class:test; style:{color:brown; font-family:Verdana, Geneva, Tahoma, sans-serif;}}
    

    The following is the Regex replace methods I'm currently using but it seems Javascript does not recognize "'$1':"

    which is meant to represent the matched string. It inserted "'$1':" verbatim.

    my_string.replace(/\:(.*?)\;/g, ":'$1';").replace(/';/g, ";',")
    .replace(/\w*\-\w*\:/g, "'$1':").replace(/\s\w*:/g, "'$1':").replace(/\;/g, "").replace(/\,}/g, "}");

    I would like the result to look like the following string if everything works:

    {'class':'test', 'style':{'color':'brown', 'font-family':'Verdana, Geneva, Tahoma, sans-serif'}}

    Sunday, May 27, 2018 7:26 AM

Answers

  • User1928065626 posted

    Hi, thanks for replying. I am trying to convert any key value pair that are not currently surrounded by double quotes into a valid JSON.

    I believe your statement "There is no such regular expression to add each key and value pair by single quotes" is wrong and it might be because you do not understand my question.

    I am not trying to add key value pairs as you have stated. I'm simply trying to wrap double quotes around each key and each value.

    I finally got it to work, below is the working REGEX:

    var result = '[';
    result += myString.replace(/\:(.*?)\;/g, ':"$1";').replace(/";/g, ';",').replace(/\;/g, "").replace(/\,}/g, "}").replace(/\,(.?)\:/g, '{"$1').replace(/\{\s*(.?)\s*/g, '{"$1'); result += result.replace(/\",/g, '", "').replace(/\:/g, '":').replace(/\'\s/g, '"').replace(/:\s*\"/g, ":").replace(/:/g, ':"').replace(/" style":/g, '"style":{');
    result += ']';

    If you are wondering why I didn't use Javascript .stringify method to convert the string into a JSON string, the answer is compound word key value pair separated by spaces or commas as seen below

    font-family: serif, Verdana, Geneva; 
    
    OR
    
    Left: 6px 10px 5px 10px;

    Javascript .parse or stringify does not recognize these.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, May 28, 2018 8:03 AM

All replies

  • User36583972 posted


    Hi madjester,

    >>I would like the result to look like the following after replacing the semicolons with commas:

    The following is the Regex replace methods I'm currently using but it seems Javascript does not recognize "'$1':"

    which is meant to represent the matched string. It inserted "'$1':" verbatim.

    my_string.replace(/\:(.*?)\;/g, ":'$1';").replace(/';/g, ";',")
    .replace(/\w*\-\w*\:/g, "'$1':").replace(/\s\w*:/g, "'$1':").replace(/\;/g, "").replace(/\,}/g, "}");

    I would like the result to look like the following string if everything works:

    {'class':'test', 'style':{'color':'brown', 'font-family':'Verdana, Geneva, Tahoma, sans-serif'}}

    It seems you want to use JavaScript convert these characters. There is no such regular expression to add each key and value pair by single quotes. You may need to find all words and combination splicing.

    I found the CSS-JSON Converter for JavaScript may helpful you.

    Best Regards,

    Yong Lu

    Monday, May 28, 2018 6:27 AM
  • User1928065626 posted

    Hi, thanks for replying. I am trying to convert any key value pair that are not currently surrounded by double quotes into a valid JSON.

    I believe your statement "There is no such regular expression to add each key and value pair by single quotes" is wrong and it might be because you do not understand my question.

    I am not trying to add key value pairs as you have stated. I'm simply trying to wrap double quotes around each key and each value.

    I finally got it to work, below is the working REGEX:

    var result = '[';
    result += myString.replace(/\:(.*?)\;/g, ':"$1";').replace(/";/g, ';",').replace(/\;/g, "").replace(/\,}/g, "}").replace(/\,(.?)\:/g, '{"$1').replace(/\{\s*(.?)\s*/g, '{"$1'); result += result.replace(/\",/g, '", "').replace(/\:/g, '":').replace(/\'\s/g, '"').replace(/:\s*\"/g, ":").replace(/:/g, ':"').replace(/" style":/g, '"style":{');
    result += ']';

    If you are wondering why I didn't use Javascript .stringify method to convert the string into a JSON string, the answer is compound word key value pair separated by spaces or commas as seen below

    font-family: serif, Verdana, Geneva; 
    
    OR
    
    Left: 6px 10px 5px 10px;

    Javascript .parse or stringify does not recognize these.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, May 28, 2018 8:03 AM