【发布时间】:2017-12-26 18:34:28
【问题描述】:
是否可以测试字符串是否为template literal?
类似:
const x = "foo"
const y = `${x}bar` // "foobar"
isTemplateLiteral(x) // false
isTemplateLiteral(y) // true
【问题讨论】:
-
不,这是不可能的。您可能得到的最接近的方法是将此代码包装到一个函数中,然后解析其源代码(可通过 toString()-ing 获得)。不过,它闻起来像 XY 问题。
-
最好询问字符串是否由模板字面量构成,因为
x和y始终是字符串。
标签: javascript string syntax template-literals