Last Updated: February 25, 2016
·
1.518K
· jakebellacera

Ruby Regexp: \A, \z, ^ and $

string = "my dog is\n5 years old"

string[/\A\d+/] # match a sequence of digits at the start of the string
# => nil
string[/^\d+/]  # match a sequence of digits at the start of a line
# => "5"

string[/\w+\z/] # match a sequence of characters at the ending of the string
# => "old"
string[/\w+$/]  # match a sequence of characters at the ending of a line
# => "is"