Journal Archives About Books Talks
til

TIL: \A > ^

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/
26
>> "javascript:alert(123);\n/*\nhttp\n*/" =~ /\Ahttp/
nil

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

# Posted on