For my small application, I am working on creating a regex pattern to identify "faux" html tags. The goal is to capture these tags within curly brackets and output them into an array of objects.
Below is the code snippet with the current regex pattern:
{
{key : value},
{key : value}
}
let str = "{p}This is a paragraph{/p} {img}(path/to/image) {ul}{li}This is a list item{/li}{li}Another list item{/li}{/ul}";
let regex = /\{(\w+)}(?:\()?([^\{\)]+)(?:\{\/1})?/g;
let match;
let matches = [];
while (match = regex.exec(str)) {
matches.push({ [match[1]]: match[2]})
}
console.log(matches)
I now need the regex pattern to handle nested groups as well and format them into an array. For example, for the given string above, the desired result would be:
[
{p : "This is a paragraph"},
{img : "path/to/image"},
{ul : ["This is a list item", "Another List item"]}
]
The objective is to match each tag in sequence so that they correspond to their order of appearance in the string.
If you have any suggestions on how I could adjust the regex pattern for this purpose, I would greatly appreciate your input. Thank you!