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"
Written by Jake Bellacera
Related protips
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Ruby
Authors
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#