RegExp test method string length limitation RangeError maximum call stack size exceeded

I stumbled upon a string length limitation for the RegExp test method.
In fact, when I want to test whether a string is a valid Base64, I use this script.

const BASETEST = /^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$/gm;
BASETEST.lastIndex = 0;
BASETEST.test(''.padEnd(100,'A'))
// expected output: true

This works fine up until a string length of 4473905. When I want to parse a Base64 string that is longer, a RangeError is returned:

BASETEST.test(''.padEnd(4473906,'A'))
// returns Uncaught RangeError RangeError: Maximum call stack size exceeded at eval (repl:1:6)

Do you agree with me and what would be the way to get this fixed?

This is my very first topic in this group so please be patient with me.
Regards,

is there a reason not to just, try to decode it using atob?

1 Like

Thanks for the reply!

atob will definitely work in this scenario and I will also use it.

However in a different situation I want to make sure that RegExp does not cause the same issue on larger strings (text files).