我有这样的字符串:
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 template literal .
let item = {
ITEMNAME : 'Foo',
ITEMWIDTH : '100%'
}
let str = `This is item: ${item.ITEMNAME} and its width is: ${item.ITEMWIDTH}...`
console.log(str)
关于javascript - 有没有更快的方法在字符串中查找占位符模板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59356572/