如何优化正则表达式?

我正在尝试运行以下 regex ^(.*\s)*(([A-Z]{2,3}(?:-[0-9]+){3}))((\s)+.*)?$,但我得到了大约 100+ 个字符的字符串超时。我在 JS 中运行了这个正则表达式,我也尝试在 regex101.com 中运行它。


我试图匹配的是“ABC-12312-121231-12312”,它可以以任何字符串开头和后跟。


这是我的 JS 代码:


const regexTransaction   = /^(.*\s)*(([A-Z]{2,3}(?:-[0-9]+){3}))((\s)+.*)?$/

var resTransaction       = new RegExp(regexTransaction,'g').exec(encodedMsg)


console.log(resTransaction)

我真的很感激任何帮助。


慕码人8056858
浏览 111回答 1
1回答

紫衣仙女

这两者似乎都具有相当的性能。第一个假设每个块中的数字数量已知(5、6、5)。第二个是原始正则表达式的清理版本,它接受 3 个由连字符分隔的任意数量的数字序列。// assumes ABC - 5 digits - 6 digits - 5 digitsconst regex = /([A-Z]{2,3}-[\d]{5}-[\d]{6}-[\d]{5})/g;// your original regex with unnecessary stuff stripped out.// notice that this captures the second item with 999 appended that the first expression doesn't get.const regex2 = /([A-Z]{2,3}(?:-[0-9]+){3})/g;const input = "asfsfdasABC-12312-121231-12312afsasfsdfdsasfsfdasABC-12312-121231-12312999afsasfsdfdsasfsfdasABC-12312-121231-12312afsasfsdfdsasfsfdasABC-12312-121231-12345afsasfsdfdsasfsfdasABC-12312-121231-12312afsasfsdfdsasfsfdasABC-12312-121231-12312afsasfsdfdsasfsfdasABC-12312-121231-12312afsasfsdfdsasfsfdasABC-12312-121231-12312afsasfsdfdsasfsfdasABC-12312-121231-12312afsasfsdfdsasfsfdasABC-99999-121231-12312afsasfsdfdsasfsfdasABC-12312-121231-12312afsasfsdfdsasfsfdasABC-12312-121231-12312afsasfsdfdsasfsfdasABC-12312-121231-12312afsasfsdfdsasfsfdasABC-12312-121231-12312afsasfsdfdsasfsfdasABC-12312-121231-12312afsasfsdfdsasfsfdasABC-12312-121231-12312afsasfsdfdsasfsfdasABC-12312-121231-12312afsasfsdfdsasfsfdasABC-12312-121231-12312afsasfsdfdsasfsfdasABC-12312-121231-12312afsasfsdfdsasfsfdasABC-12312-121231-12312afsasfsdfdsasfsfdasABC-12312-121231-12312afsasfsdfdsasfsfdasABC-12312-121231-12312afsasfsdfdsasfsfdasABC-12312-121231-12312afsasfsdfdsasfsfdasABC-12312-121231-12312afsasfsdfdsasfsfdasABC-12312-121231-12312afsasfsdfdsasfsfdasABC-12312-121231-12312afsasfsdfdsasfsfdasABC-12312-121231-12312afsasfsdfdsasfsfdasABC-12312-121231-12312afsasfsdfdsasfsfdasABC-12312-121231-12312afsasfsdfdsasfsfdasABC-12312-121231-12312afsasfsdfdsasfsfdasABC-12312-121231-12312afsasfsdfdsasfsfdasABC-12312-121231-12312afsasfsdfdsasfsfdasABC-12312-121231-12312afsasfsdfdsasfsfdasABC-12312-121231-12312afsasfsdfdsasfsfdasABC-12312-121231-12312afsasfsdfdsasfsfdasABC-12312-121231-12312afsasfsdfdsasfsfdasABC-12312-121231-12312afsasfsdfdsasfsfdasABC-12312-121231-12312afsasfsdfdsasfsfdasABC-12312-121231-12312afsasfsdfdsasfsfdasABC-12312-121231-12312afsasfsdfdsasfsfdasABC-12312-121231-12312afsasfsdfdsasfsfdasABC-12312-121231-12312afsasfsdfdsasfsfdasABC-12312-121231-12312afsasfsdfdsasfsfdasABC-12312-121231-12312afsasfsdfdsasfsfdasABC-12312-121231-12312afsasfsdfdsasfsfdasABC-12312-121231-12312afsasfsdfdsasfsfdasABC-12312-121231-12312afsasfsdfdsasfsfdasABC-12312-121231-12312afsasfsdfdsasfsfdasABC-12312-121231-12312afsasfsdfdsasfsfdasABC-12312-121231-12312afsasfsdfdsasfsfdasABC-12312-121231-12312afsasfsdfdsasfsfdasABC-12312-121231-12312afsasfsdfdsasfsfdasABC-12312-121231-12312afsasfsdfdsasfsfdasABC-12312-121231-12312afsasfsdfds";console.log(input.match(regex));console.log(input.match(regex2));
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript