javascript - 有没有更快的方法在字符串中找到占位符模板
问题描述
我有这样的字符串:
This is item: {item.ITEMNAME} and it's width is: {item.ITEMWIDTH}...
我需要获取所有这些占位符,但现在我正在手动进行,例如:
if (indexOf('{item.') > 0) {
// get a list of all placeholders
// Loop through all and try to replace
}
这可能很慢,有没有办法让它更通用并提取所有内容:
let array = string.find('{item.???}');
解决方案
你能在每个左花括号前附加一个美元符号吗?
然后你可以使用 ES6模板文字。
let item = {
ITEMNAME : 'Foo',
ITEMWIDTH : '100%'
}
let str = `This is item: ${item.ITEMNAME} and its width is: ${item.ITEMWIDTH}...`
console.log(str)
推荐阅读
- android - 如何在没有 GPS 的情况下获取 Cell ID 和 LAC?
- html - 美丽的汤获取表单提交后返回的html表单数据
- java - 房间一对多警告
- angular - Mapbox 未在 Stackblitz 中显示,但在本地计算机中显示
- android - 如何检查 arcore 图像数据库中的内容?
- javascript - 在 fullcalendar.io 上重新渲染,删除所有事件
- python - 使用 RDKIT:以整数/计数形式生成指纹
- reactjs - 日期排序在 muidatatable 中不起作用
- bind9 - Bind9 将 DNS 查询转发到 2 个不同的 DNS 服务器 | 内部 <-> dmz <-> 外部
- sql - 如何检查日期是否在并行时间间隔内?