用json对象替换给定字符串中的变量

时间:2017-02-25 07:29:18

标签: javascript

我有以下示例模板我想用json对象值替换baby.name如何做到这一点请帮我解决这个问题。

if($rowcount1 > 0){
        ?>
            <h3 class="text-muted"><u>DEPARTURE</u></h3>
            <h4><?php echo "$origin to $destination "?></h4>
                <table class="table table-hover">
                  <thead>
                    <tr>
                      <th>FLIGHT NO.</th>
                      <th>DEPART</th>
                      <th>ARRIVE</th>
                      <th>AIRPORT</th>
                      <th>DURATION</th>
                      <th>FLY ONLY</th>
                      <th>FLY + BAGGAGE</th>
                    </tr>
                  </thead>
                  <tbody>
                     <?php
                        foreach ($query as $flightr) {
                            echo "<tr>";
                            echo "<td>".$flightr['id']."</td>";
                            echo "<td>".$flightr['depart']."</td>";
                            echo "<td>".$flightr['arrive']."</td>";
                            echo "<td>".$flightr['airport']."</td>";
                            echo "<td>".$flightr['duration']."</td>";
                            echo "<td> <label for=lbl7> <input type='radio' checked id='lbl7' name='flight[]' value='".$flightr['flyonly']."'> PHP ".number_format($flightr['flyonly'])."</label></td>";
                            echo "<td> <label for=lbl8> <input type='radio' checked id='lbl8' name='flight[]' value='".$flightr['flybaggage']."'> PHP ".number_format($flightr['flybaggage'])."</label></td>";
                            echo "</tr>";
                        }
                     ?>
                  </tbody>
                </table>
                <hr>
                    <?php

                        }else{
                            echo " <div class='alert alert-info' role='alert'>
                                       No results found on <BIG class='text-muted'>Departure</BIG> pick another flight thank you.
                                    </div>
                            ";
                        }
        }
        }
    ?>
    <button type="button" name="forform" onclick="" class="hit">hit</button>

借助字符串替换需要实现此目的。 输出想要这样&#34;欢迎来到世界婴儿小腿&#34;

2 个答案:

答案 0 :(得分:0)

您可能需要查看template literals

  

模板文字是允许嵌入表达式的字符串文字。您可以使用多行字符串和字符串插值功能。他们被称为&#34;模板字符串&#34;在ES2015规范的先前版本中。

string text ${expression} string text

所以你的案子变成了

var Template = 'Welcome to the world baby ${namelist[0].baby.name}'

答案 1 :(得分:0)

您可以使用RegExp /\{{2}(.+)\}{2}/来匹配"{{"一个或多个字符,例如"baby.name""}}".split()和{ {1}}作为获取".""baby"的参数,使用括号表示法或解构赋值来获取"name"数组中对象的属性

&#13;
&#13;
namelist
&#13;
var Template = 'Welcome to the world baby {{baby.name}}';

const namelist= [{school:"hakvoz",baby: { name: 'shanker' }}];

let template = Template.replace(/\{{2}(.+)\}{2}/, function(match, p1) {
  let [prop, key] = p1.split(".");
  return namelist[0][prop][key];
});

document.querySelector("pre").textContent = template;
&#13;
&#13;
&#13;