如何将Javascript引用传递回键或函数? //我有模板和信息 var img_template=“”; var img_信息={ src:'http://myimage.com/img.jpg', 宽度:“100px”, 高度:“100px”, 标题:“我的形象” } //我想把信息模板,但它不是工作。 //我该怎么办? var my_image=img_template.replace(/{(+?)}/g,img_info['$1']);

如何将Javascript引用传递回键或函数? //我有模板和信息 var img_template=“”; var img_信息={ src:'http://myimage.com/img.jpg', 宽度:“100px”, 高度:“100px”, 标题:“我的形象” } //我想把信息模板,但它不是工作。 //我该怎么办? var my_image=img_template.replace(/{(+?)}/g,img_info['$1']);,javascript,regex,backreference,Javascript,Regex,Backreference,使用替换功能: <script type='text/javascript'> // I have template and info var img_template = "<img src='{src}' width='{width}' height='{height}' title='{title}' />"; var img_info = { src : 'http://myimage.com/img.jpg', width: '100px',

使用替换功能:

<script type='text/javascript'>

 // I have template and info
 var img_template = "<img src='{src}' width='{width}' height='{height}' title='{title}' />";
 var img_info = {
  src : 'http://myimage.com/img.jpg',
  width: '100px',
  height: '100px',
  title: 'My Image'
 }

 // I want to put info to template but It's not work.
 // How should I do ?
 var my_image = img_template.replace(/{(.+?)}/g, img_info['$1']);

</script>

var img_template=“”;
var img_信息={
src:'http://myimage.com/img.jpg',
宽度:“100px”,
高度:“100px”,
标题:“我的形象”
}
var my_image=img_template.replace(/{(+?)}/g,函数(a,b){
返回img_信息[b];
});
举例


有关使用

的详细信息您需要为
replace()
使用回调函数

var img_template=”“;
var img_信息={
src:'http://myimage.com/img.jpg',
宽度:“100px”,
高度:“100px”,
标题:“我的形象”
};
//将为每个匹配执行回调函数
var my_image=img_template.replace(/{([^}]+)}/g,函数(match,group1){
//返回查找值或空字符串
返回img|u信息[group1]| |“”;
});
或者,以可重复使用的形式:

var img_template = "<img src='{src}' width='{width}' height='{height}' title='{title}' />";
var img_info = {
  src : 'http://myimage.com/img.jpg',
  width: '100px',
  height: '100px',
  title: 'My Image'
};

// callback function will be executed for each match
var my_image = img_template.replace(/{([^}]+)}/g, function(match, group1) {
  // return lookup value or the empty string
  return img_info[group1] || "";
});
函数HtmlTemplate(html){
this.template=html;
this.render=函数(信息){
返回此.template.replace(/{([^}]+)}/g,函数(匹配,组1){
返回信息[组1]| |“”;
});
};
}
var imgTemplate=新的HtmlTemplate(“”);
//后来
var img=imgTemplate.render(img_info);
var my_image = img_template.replace(/{(.+?)}/g, function(m,v){return img_info[v];});
var my_image = img_template.replace(/{(.+?)}/g, function(match, group1){
  return img_info[group1];
});
var img_template = "<img src='{src}' width='{width}' height='{height}' title='{title}' />";
var img_info = {
  src : 'http://myimage.com/img.jpg',
  width: '100px',
  height: '100px',
  title: 'My Image'
};

// callback function will be executed for each match
var my_image = img_template.replace(/{([^}]+)}/g, function(match, group1) {
  // return lookup value or the empty string
  return img_info[group1] || "";
});
function HtmlTemplate(html) {
  this.template = html;
  this.render   = function(info) {
    return this.template.replace(/{([^}]+)}/g, function(match, group1) {
      return info[group1] || "";
    });
  };
}

var imgTemplate = new HtmlTemplate("<img src='{src}' width='{width}' height='{height}' title='{title}' />");

// later

var img = imgTemplate.render(img_info);