質問

In my rails application I am searching for company name and I am using mongodb for backend. My query for finding company is

@companies = Company.where(name: /#{params[:search]}/i).all

In my db I have company having name "ABC&D Services Inc. (Atlanta Project)" and When I am trying to search this company with giving string "ABC&D Services Inc." it will not return any result. Also if I am trying to search with string "ABC&D Services Inc.(" it gives me error end pattern with unmatched parenthesis

Anyone have idea how to solve this kind of issues with reg-ex. End user can perform search with any input so there is way to handle this?

役に立ちましたか?

解決

Regexp.escape escapes special characters:

@companies = Company.where(name: /#{Regexp.escape(#{params[:search]})}/i).all

他のヒント

@companies = Company.where(name: /#{Regexp.escape(params[:search])}/i)
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top