TIL: \A > ^

  Wynn Netherland • January 10, 2013

Whether it be in Vim, JavaScript, or Ruby, ^ is a go-to pattern for matching the beginning of a line. As Ben Toews schooled me today, if you’re needing to match the beginning of user input, it’s better to use \A which matches the beginning of a string:

>> "javascript:alert(123);\n/*\nhttp\n*/" =~ /^http/
>> "javascript:alert(123);\n/*\nhttp\n*/" =~ /\Ahttp/

There’s also \Z, something to keep in mind the next time you use validates_format_of in Rails.