为了账号安全,请及时绑定邮箱和手机立即绑定

如何优化正则表达式?

如何优化正则表达式?

慕码人8056858 2021-06-02 17:09:43
我正在尝试运行以下 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)我真的很感激任何帮助。
查看完整描述

1 回答

?
紫衣仙女

TA贡献1839条经验 获得超15个赞

这两者似乎都具有相当的性能。第一个假设每个块中的数字数量已知(5、6、5)。第二个是原始正则表达式的清理版本,它接受 3 个由连字符分隔的任意数量的数字序列。


// assumes ABC - 5 digits - 6 digits - 5 digits

const 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));


查看完整回答
反对 回复 2021-06-03
  • 1 回答
  • 0 关注
  • 110 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信