gpt4 book ai didi

javascript - 如何优化正则表达式?

转载 作者:行者123 更新时间:2023-12-02 23:38:43 25 4
gpt4 key购买 nike

我正在尝试运行以下正则表达式 ^(.*\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)

我非常感谢您的帮助。

最佳答案

这两个看起来都表现得相当不错。第一个假设每个 block 中的数字数量已知(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));

关于javascript - 如何优化正则表达式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56179333/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com