Strictly speaking, I don't see the bug either.
But depending on the regEx implementation, I can see how the error can happen.
Side note: The both regEx are wrong, and the second is also dirty.
Declaration:
[...]persistent\.([a-zA-Z]+[a-zA-Z0-9_]*)\s*=[...]
A dot, [start capture] followed by at least one letter (uppercase or lowercase), possibly followed by as many letter (uppercase or lowercase) or a digit or an underscore, as there is [stop capture] until it will find an equal sign, with possibly one or more space before it.
Error: "persistent._variable" is a valid syntax, but wouldn't be caught.
Check:
[...]persistent\.(\w+)[^a-zA-Z]/g
A dot, [start capture] followed by at least a letter (uppercase or lowercase) or an underscore or a digit, [stop capture] followed by everything except a letter (uppercase or lowercase).
Error: will caught the invalid "persistent.3variable".
Now for the possible cause of the error:
Normally the
\w+
should be greedy. If you have by example "bla_bla_345678 " it should catch everything, because it match the
\w
, until the "space" because it don't match the
\w
AND match the
[^a-zA-Z]
.
But, as the error triggered show, it's not what happen. The first underscore will be seen as part of the
[^a-zA-Z]
, not caring about the fact that it's also part of the
\w
, what will stop the capture.
The correct regEx should be:
const rxPersistentDefines = /^\s*(default|define)\s+persistent\.([a-zA-Z_]\w*)\s*=\s*(.*$)/g;
Will be captured anything that starts with a letter (uppercase or lowercase) or an underscore, possibly followed by one or more letter (uppercase or lowercase) or underscore or digit, until it will find an equal sign, with possibly one or more space before it.
const rxPersistentCheck = /\s+persistent\.([a-zA-Z_]\w*)\W/g;
Same as above, but the capture will stop when it will encounter the first character that isn't a letter (uppercase or lowercase) or an underscore or a digit.
Normally with those two regEx it should works. But since the previous one failed due to the regEx implementation, I can't guaranty it at 100%.
I built them taking count of the none greedy issue, but I don't guaranty that the implementation don't trigger others issues with this formulation.