惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

月光博客
月光博客
IT之家
IT之家
Hugging Face - Blog
Hugging Face - Blog
J
Java Code Geeks
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
博客园 - 叶小钗
MyScale Blog
MyScale Blog
G
Google Developers Blog
Microsoft Azure Blog
Microsoft Azure Blog
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
大猫的无限游戏
大猫的无限游戏
博客园 - 三生石上(FineUI控件)
Google DeepMind News
Google DeepMind News
Engineering at Meta
Engineering at Meta
The Cloudflare Blog
Martin Fowler
Martin Fowler
酷 壳 – CoolShell
酷 壳 – CoolShell
N
Netflix TechBlog - Medium
MongoDB | Blog
MongoDB | Blog
I
InfoQ
WordPress大学
WordPress大学
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
H
Help Net Security

小涂博客

mTab书签一些基础的操作教程 分享一个简化服务器管理的在线ssh工具利器-WebTerm 蓝易云SCDN — 强大的安全防护CDN,为你的网站提供全方位防护! VsCode的EasyLess扩展的setting.json配置 js复制图片到粘贴板 Ubuntu 22.04版本中,root用户登录、硬盘挂载等新机器操作指南 Linux中iostat命令 Redis基本命令和常用数据类型 为普通用户授权访问k8s资源(tls,rbac)
JS字符串模板替换一般用于国际化中含有动态书内容使用
涂山 · 2023-12-13 · via 小涂博客

涂山 web前端 阅读 3.39k

有时候国际化或者一段动态的支付模板或者短信模板配置中需要一些动态的参数,可以用下面的方法来动态渲染部分需要变化的字符串的内容。

const myRender = (template, person) => {
  let reg = /{{(.*?)}}/g

  let res = template.replace(reg, (item, key) => {
    return person[key]
  })

  return res
}

let template = '我是{{name}},年龄{{age}},性别{{sex}}'
let person = {
  name: '布兰',
  age: 12,
}

let res = myRender(template, person)
console.log(res) // 我是布兰,年龄12,性别undefined