我想匹配以下字符串作为前缀。
客户
客户端
代理
如果我的字符串包含上述任何内容,我想忽略。
let str = "customer_service"
if(!str.startsWith("customer") && !str.startsWith("client") && !str.startsWith("agent"))
return true;
有什么简单有效的解决方案吗?提前致谢。
最佳答案
您可以根据正则表达式测试字符串。如果不匹配则继续,否则忽略。
const regex = /^(customer|client|agent).*/;
const str = "customer_service";
if (!regex.test(str)) {
// Ignored.
// Do whatever you want to do
}
关于javascript - 如何在javascript中使用多个字符串来匹配字符串前缀?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61126585/