diff --git a/Library/Homebrew/search.rb b/Library/Homebrew/search.rb index 1a695ff620..fdd90f3ed6 100644 --- a/Library/Homebrew/search.rb +++ b/Library/Homebrew/search.rb @@ -4,7 +4,7 @@ module Homebrew if m = query.match(%r{^/(.*)/$}) Regexp.new(m[1]) else - /.*#{Regexp.escape(query)}.*/i + Regexp.new(query.chars.join('[^a-z\d]*'), Regexp::IGNORECASE) end rescue RegexpError raise "#{query} is not a valid regex." diff --git a/Library/Homebrew/test/search_spec.rb b/Library/Homebrew/test/search_spec.rb index 26236af472..f7be0b805f 100644 --- a/Library/Homebrew/test/search_spec.rb +++ b/Library/Homebrew/test/search_spec.rb @@ -55,11 +55,26 @@ describe Homebrew::Search do end it "correctly converts a query string to a regex" do - expect(mod.query_regexp("query")).to eq(/.*query.*/i) + expect(mod.query_regexp("query")).to eq(/q[^a-z\d]*u[^a-z\d]*e[^a-z\d]*r[^a-z\d]*y/i) end it "raises an error if the query is an invalid regex" do expect { mod.query_regexp("/+/") }.to raise_error(/not a valid regex/) end + + it "correctly matches with special symbols" do + regex = mod.query_regexp("oo-ba") + expect(regex).to match("foo-bar") + end + + it "correctly matches without special symbols" do + regex = mod.query_regexp("ooba") + expect(regex).to match("foo-bar") + end + + it "keeps special symbols" do + regex = mod.query_regexp("foo-bar") + expect(regex).not_to match("foobar") + end end end