Erioifpuderio.hashnode.dev·May 9, 2024Javascript 正则需要注意的问题前情提要 时隔多年,又被这个问题坑了一次,事情是这样的,我在一个需求里使用 exec 来验证用户的输入,像这样: const text = /* 用户输入 */ const regex = /^[A-L](上|下|左|右)$/g const match = regex.exec(text) // 后续根据 match 是否为 null 判断匹配情况 结果呢,用户第一次输入“A上”,没有问题,第二次输入“H左”,没匹配上。 问题 关键在于正则的 global 描述符,也就是最后的 g,如果不使...JavaScriptAdd a thoughtful commentNo comments yetBe the first to start the conversation.