Как я могу реализовать следующую функцию javascript, которая принимает значения из текста по указанному шаблону
Coding algorithms in JavaScript:
Write function, which will take values from text by specified template.
Function:
function parse (template, text) {
// TODO: Implement logic
return result;
}
Template:
div class="content">
[{TITLE}]
<ul class="attributes">
Attributes:
<li>Size: [{SIZE}]</li>
<li>Сolor: [{COLOR}]</li>
</ul>
<img src="[{IMAGE1}]" />
<img src="[{IMAGE2}]" />
</div>
Source text:
<div class="content">
Adidas shoes new for women
<ul class="attributes">
Attributes:
<li>Size: 36</li>
<li>Сolor: Red</li>
</ul>
<img src="http://www.site.com/adidas-busenitz-vulc-skate-shoes-black-running-red.jpg" />
<img src="http://www.site.com/adidas-busenitz-vulc-skate-shoes-black-running-red2.jpg" />
</div>
Result:
[
{
tag: "[{TITLE}]",
value: "Adidas shoes new for women"
},
{
tag: "[{SIZE}]",
value: "36"
},
{
tag: "[{COLOR}]",
value: "Red"
},
{
tag: "[{IMAGE1}]",
value: "http://www.site.com/adidas-busenitz-vulc-skate-shoes-black-running-red.jpg"
},
{
tag: "[{IMAGE2}]",
value: "http://www.site.com/adidas-busenitz-vulc-skate-shoes-black-running-red2.jpg"
}
]
NB! Script should be written on pure JavaScript.
What I have tried:
This is completely new to me and I am not sure how to proceed.