如何实现以下javascript函数,该函数从指定模板的文本中获取值 [英] How can I implement the following javascript function that takes values from text by specified template

查看:66
本文介绍了如何实现以下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.

推荐答案

您的模板说:您有3个变量;由[{和}]分隔。



但是,您的结果显示模板中没有的额外变量。



所以,你可以回到任何人,并告诉他们他们的规格是不完整的。
Your "template" "says": you have 3 "variables"; delimited by "[{" and "}]".

However, your "result" shows "extra" variables not in your template.

So, you can go back to whomever, and tell them their "specs" are incomplete.


Aplogies,添加问题时一定会发生这种情况。我已使用正确的信息更新了问题。
Aplogies, this must have happened when adding the question. I have updated the question with the right information.


这篇关于如何实现以下javascript函数,该函数从指定模板的文本中获取值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆