Difference between \A \z and ^ $ in Ruby regular expressions

If you’re depending on the regular expression for validation, you always want to use \A and \z. ^ and $ will only match up until a newline character, which means they could use an email like [email protected]\n<script>dangerous_stuff();</script> and still have it validate, since the regex only sees everything before the \n.

My recommendation would just be completely stripping new lines from a username or email beforehand, since there’s pretty much no legitimate reason for one. Then you can safely use EITHER \A \z or ^ $.

Leave a Comment