Javascript 中的等效正则表达式

Equivalent regex in Javascript

我正在尝试验证 AWS accessKey 和 secretKey。找到一个 AWS 博客 post,其中建议使用正则表达式来验证 accessKey 和 secretKey https://blogs.aws.amazon.com/security/post/Tx1XG3FX6VMU6O5/A-safer-way-to-distribute-AWS-credentials-to-EC2

但是他们建议的正则表达式包括负向后视,我们知道 java-script 不支持正则表达式中的负向后视。所以任何人都可以在 JavaScript.

中建议一个等效的正则表达式
AccessKey: (?<![A-Z0-9])[A-Z0-9]{20}(?![A-Z0-9])
This means find me 20-character, uppercase, alphanumeric strings that don’t have any uppercase, alphanumeric characters immediately before or after.

SecretKey: (?<![A-Za-z0-9/+=])[A-Za-z0-9/+=]{40}(?![A-Za-z0-9/+=]) 
This means find me 40-character, base-64 strings that don’t have any base 64 characters immediately before or after.

示例字符串:https://regex101.com/r/wn7t9D/3

任何 help/suggestion 真的很感激。

谢谢 鲁曼

您可以在 A-Z0-9 范围内交替使用字符串开头和取反字符 class 而不是负向回顾:

访问密钥:

(^|[^A-Z0-9])[A-Z0-9]{20}(?![A-Z0-9])
^^^^^^^^^^^^^

regex demo

密钥:

(^|[^A-Za-z0-9/+=])[A-Za-z0-9/+=]{40}(?![A-Za-z0-9/+=]) 
^^^^^^^^^^^^^^^^^^^

Another regex demo