diff --git a/lib/email_spec/helpers.rb b/lib/email_spec/helpers.rb
index 62334a49..1ac933ab 100644
--- a/lib/email_spec/helpers.rb
+++ b/lib/email_spec/helpers.rb
@@ -140,9 +140,11 @@ def parse_email_for_explicit_link(email, regex)
request_uri(url)
end
+ HrefRegex = %r{href=['"]?([^'"]*)['"]?}
+ NotClosingARegex = %r{(?:(?!).)*?}
# e.g. Click here in Click here
def parse_email_for_anchor_text_link(email, link_text)
- if textify_images(email.default_part_body) =~ %r{]*href=['"]?([^'"]*)['"]?[^>]*?>[^<]*?(.*?)#{link_text}(.*?)[^<]*?}
+ if textify_images(email.default_part_body) =~ %r{]*#{HrefRegex}[^>]*>#{NotClosingARegex}#{link_text}#{NotClosingARegex}}
URI.split($1)[5..-1].compact!.join("?").gsub("&", "&")
# sub correct ampersand after rails switches it (http://dev.rubyonrails.org/ticket/4002)
else
diff --git a/spec/email_spec/helpers_spec.rb b/spec/email_spec/helpers_spec.rb
index 324170e3..3b17ab90 100644
--- a/spec/email_spec/helpers_spec.rb
+++ b/spec/email_spec/helpers_spec.rb
@@ -31,7 +31,9 @@
end
it "properly finds links with text surrounded by tags" do
- email = Mail.new(:body => %(Click Here))
+ email = Mail.new(:body => %(
+ WelcomeClick Here))
+
expect(parse_email_for_link(email, "Click Here")).to eq("/path/to/page")
end