string.startsWith()通配符或正则表达式

问题描述 投票:0回答:1

重构一些在JavaScript中使用string.startsWith()的代码。 Docs不要说您可以使用通配符或正则表达式。什么是替代方法?

javascript node.js ecmascript-5
1个回答
1
投票

[string.prototype.matchregex.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);
© www.soinside.com 2019 - 2024. All rights reserved.