I found this difficult to explain (and search for) so let me try with an example.
The goal is a template system for a nodejs backend where I can replace custom tags in an HTML template with specific content based on certain params such as language.
A simple replace example:
<h1>{static||header}</h1>
My custom tag here can be replaced with:
template = template.replace(/{static\|\|(.*?)}/g, (match, $1) => { /* find the variable to replace $1 ("header") with and return */});
A more complex example where I loop through an array:
<ul>{static||items||loop||<li id="%k">%v</li>}</ul>
the code to replace it:
template = template.replace(/{static\|\|(.*?)}/g, (match, $1) => {
// static parts
let parts = $1.split("||");
if(parts[1] === "loop"){
/* loop through the "items" array replacing %k and %v as appropriate in parts[2] */
}
});
But now I have run into a new challenge I need to solve and my regex ability is somewhat lacking to solve this so I am turning to StackOverflow for ideas.
The problem I need to solve is a loop, as above, but within that loop are more tags, these could be simple replacements or looped replacements. The point is, the regex needs to find the closing } for the outer tag and ignore any tags and variables that are within it.
Html example:
<ul>{static||items||loop||<li id="%k">
<strong>{static||foo}: %v</strong>
<ul>{static||subitems||loop||<li>%v</li>}</ul>
</li>}</ul>
So the regex should match the first "{static" to the last "}" and ignore the 2 "{static...}" that are within it. How can the regex "know" that the correct closing "}" is the last one?
Many thanks!