如何优化正则表达式?

时间:2019-05-17 03:50:31

标签: javascript 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 个答案:

答案 0 :(得分:1)

这两个都表现合理。第一个假设每个块(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));