I am trying to validate AWS accessKey and secretKey. Found an AWS blog post which suggest a regex to validate accessKey and secretKey https://blogs.aws.amazon.com/security/post/Tx1XG3FX6VMU6O5/A-safer-way-to-distribute-AWS-credentials-to-EC2
But the regex they suggested includes negative lookbehind, As we know java-script doesn't support negative lookbehind in regex. So can anyone please suggest an equivalent regex in 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.
Sample String: https://regex101.com/r/wn7t9D/3
Any help/suggestion is really appreciated.
Thanks Ruman
You may use an alternation of the start of string and a negated character class with the A-Z0-9
range instead of the negative lookbehind:
AccessKey:
(^|[^A-Z0-9])[A-Z0-9]{20}(?![A-Z0-9])
^^^^^^^^^^^^^
See the regex demo
SecretKey:
(^|[^A-Za-z0-9/+=])[A-Za-z0-9/+=]{40}(?![A-Za-z0-9/+=])
^^^^^^^^^^^^^^^^^^^