重构一些在JavaScript中使用string.startsWith()的代码。 Docs不要说您可以使用通配符或正则表达式。什么是替代方法?
[string.prototype.match
和regex.prototype.test
。
let a = 'hello'.match(/^[gh]/); // truthy ('h')
let b = 'gello'.match(/^[gh]/); // truthy ('g')
let c = 'ello'.match(/^[gh]/); // falsey (null)
console.log(a, b, c);