Improved checkRegex function