I need to search for fields and values within a text and turn them into an object.
Example of text
// condition 1
<@if VERSION = "A1" || VERSION = "A3">
<@assign CTA = "blue">
<@assign CTA2 = "green">
<@assign TEXT1 = "Hello<br/>World">
<@elseif VERSION = "A2">
<@assign CTA = "red">
<@assign CTA2 = "yellow">
<@assign CTA3 = "brown">
<@assign TEXT1 = "Click <a href='https://example.com' style='text-decoration:none;color:#000000;'>here</a>">
<@else>
<@assign CTA = "black">
<@assign CTA2 = "white">
<@assign CTA3 = "pink">
</@if>
// condition 2
<@if VERSION = "A4" || VERSION = "A5">
<@assign CTA = "purple">
<@assign CTA2 = "orange">
<@assign TEXT1 = "Hi <span style='font-weight:bold;'>John</span>">
</@if>
// condition 3
<@if LANG = "en_US">
<@assign TITLE = "English">
<@else>
<@assign TITLE = "French">
</@if>
If the condition contains "@assign" must construct an object
code I'm trying
jsonObj = [];
var hidden_text = html_c.replace(/<@IF[\s\S]*?<\/@IF>/gi, function(i) {
i = i.replace(/<@IF[\s\S]*?>/gi, function(k) {
var $ogg;
item = {}
k = k.replace(/(^(?!.*@IF)|(?<=@IF)).*?((?=\=))/gi, function(x) {
x = x.replace(/^\s+|\s+$|\s+(?=\s)/g, "");
item[x] = [];
$ogg = x;
return x;
});
jsonObj.push(item);
item2 = {}
k = k.replace(/"[\s\S]*?"/gi, function(y) {
item2[y] = [];
return y;
});
item[$ogg].push(item2);
return k;
});
return i;
});
console.log(jsonObj);
<script>
const html_c = `
<@if VERSION = "A1" || VERSION = "A3">
<@assign CTA = "blue">
<@assign CTA2 = "green">
<@assign TEXT1 = "Hello<br/>World">
<@elseif VERSION = "A2">
<@assign CTA = "red">
<@assign CTA2 = "yellow">
<@assign CTA3 = "brown">
<@assign TEXT1 = "Click <a href='https://example.com'
style='text-decoration:none;color:#000000;'>here</a>">
</@if>
// condition 2
<@if VERSION = "A4" || VERSION = "A5">
<@assign CTA = "purple">
<@assign CTA2 = "orange">
<@assign TEXT1 = "Hi <span style='font-
weight:bold;'>John</span>">
</@if>
// condition 3
<@if LANG = "en_US">
<@assign TITLE = "English">
<@else>
<@assign TITLE = "French">
</@if>
`;
</script>
With this code I can create the first part of the object but I don't know how to go about it. moreover, if there are more than one condition with the same field name (e.g. VERSION) a new object is created, while I would like to make it go and update the existing one.
the result I want to get is this, considering:
"VERSION" could have any other name, the script must take the name it finds.
the values of the <@assign> variables may contain some html code
In the if and elseif conditions there could also be the double operator ==
case of the first condition
[{
"VERSION": [{
"A1": [{
"CTA": "blue",
"CTA2": "green",
"TEXT1": "Hello<br/>World",
}, ],
"A3": [{
"CTA": "blue",
"CTA2": "green",
"TEXT1": "Hello<br/>World",
}, ],
"A2": [{
"CTA": "red",
"CTA2": "yellow",
"CTA3": "brown",
"TEXT1": "Click <a href='https://example.com' style='text-
decoration:none;color:#000000;'>here</a>",
}, ],
"ELSE": [{
"CTA": "black",
"CTA2": "white",
"CTA3": "pink",
}, ],
}]
}, ]
after, if there is another condition that contains '@assign' the object must be updated:
in the case of condition 2, the field 'VERSION' already exists within the object so it will have to update by adding the values found
"A4": [{
"CTA": "purple",
"CTA2": "orange",
"TEXT1": "Hi <span style='font-
weight:bold;'>John</span>",
}, ],
"A5": [{
"CTA": "purple",
"CTA2": "orange",
"TEXT1": "Hi <span style='font-
weight:bold;'>John</span>",
}, ],
in the case of condition 3, the LANG field does not exist in the object and therefore will have to be created
"LANG": [{
"en_US": [{
"TITLE": "English",
}, ],
}]
the variables declared in the possible <@else> will go to update the already existing object "ELSE"
Final object
[{
"VERSION": [{
"A1": [{
"CTA": "blue",
"CTA2": "green",
"TEXT1": "Hello<br/>World",
}, ],
"A3": [{
"CTA": "blue",
"CTA2": "green",
"TEXT1": "Hello<br/>World",
}, ],
"A2": [{
"CTA": "red",
"CTA2": "yellow",
"CTA3": "brown",
"TEXT1": "Click <a href='https://example.com' style='text-
decoration:none;color:#000000;'>here</a>",
}, ],
"A4": [{
"CTA": "purple",
"CTA2": "orange",
"TEXT1": "Hi <span style='font-
weight:bold;'>John</span>",
}, ],
"A5": [{
"CTA": "purple",
"CTA2": "orange",
"TEXT1": "Hi <span style='font-
weight:bold;'>John</span>",
}, ],
"ELSE": [{
"CTA": "black",
"CTA2": "white",
"CTA3": "pink",
}, ],
}],
"LANG": [{
"en_US": [{
"TITLE": "English",
}, ],
"ELSE": [{
"TITLE": "French",
}, ],
}],
}, ]
UPDATE
Condition 4
If a new condition calls up an existing field and value pair, the object must update. For example:
<@if VERSION = "A1">
<@assign CTA = "black">
<@assign TEXT2 = "Hello world">
</@if>
VERSION: "A1" has already been created in the object, so it needs to be updated:
- CTA is already present within it, so the value will be replaced with "black"
- TEXT2 was not yet present, so it will be added
hidden_textwill replace all the matches withundefined..match(/regexp/gi).forEach()to iterate over the matches.imakes this code really hard to follow.@from the custom tags and ensure always closed<assign></assign>tags) where one then can parse an html document from. Querying values from a parsed DOM with element and attribute nodes is easier and more reliable.