With a string like {float: 'null', another: 'foo'}, I'd like to grab each set of key/values pairs so that the groups would output float null, and another and foo.
My current regex is /\{(?<set>(?<key>\w*)\s*\:\s*(?<value>.*)?\s?)*\}/g
It grabs the key correctly, but anything past from the comma on receives it as the value. I'm using named groups mainly just for clarity. Can't figure out how to extract each key/value pair especially when there are multiple.
Thanks for any help
Currently am trying /\{(?<set>(?<key>\w*)\s*\:\s*(?<value>.*)?\s?)*\}/g but the output is:
the group 'set': float: 'null', another: 'foo' (correct)
the group 'key': float (correct)
the group 'value': 'null', another: 'foo' (incorrect, I want just null)
Would like it to capture all key/value pairs if possible
Edit for more clarity:
My specific use case is for parsing Markdown and plugging it into custom components in Svelte, where I want to control the ability to gather props from the markdown syntax on an image. From what I've gathered online about putting attributes on an image, it should look something like:
![Alt Text]https://<fullurl>.jpg "This is hover text"){prop1: 'foo', prop2: 'bar', float: true}
Reason for regex is parsing the markdown string. It's not JSON, and I dont really gain anything by following JSON semantics ("'s on the key)
setandkey. If you are sure it is a word you could go with\w+or use the'to end the stringJSON.parse()and after that replace all'null'withnull. If needed you can then use Object.entries() on the result."foo"not'foo') and field names ("another"notanother)? (I've never even read json spec. So not 100% sure. But I know that I was often forced to replaces quotes with double quotes to avoid errors, with both python and firefox's JS. So, in practice, using json parsers wouldn't work on such a string)eval, that would be evil :D