0

I have an array of strings like so...

Array = ["WELLS FARGO DEALER  May 18 06:11 4137 Reference# 10982", 
         "ONLINE - TRANSFER TO Ult SavingMay 18 03:57 4137 ONLINE Reference# 5929", 
         "Transfer From Checking 03:57a #4137 ONLINE Reference # 005929",
         "BURLNGTON STORES861"]

When I use jquery-ui autocomplete and the user starts to type a word or words, I want the autocomplete to only show the "WORD or WORDS" that matches the input so even if the string that matches is longer it only shows the matching word or words within that string. How can I select only matching words from within strings within in an array?

So if a user types wells f it should render wells fargo from the "WELLS FARGO DEALER May 18 06:11 4137 Reference# 10982" string.

SupremeA
  • 1,519
  • 3
  • 26
  • 43
  • You can create an object having properties of possible input value matches, which reference, for example, 7 to property name length, return value of property if 7 of 11 characters match. – guest271314 Aug 12 '16 at 05:02
  • See also http://stackoverflow.com/questions/23305000/javascript-fuzzy-search-that-makes-sense/ – guest271314 Aug 12 '16 at 05:10

1 Answers1

1

You could use a regexp like /#{text}([a-zA-Z]*)\b/, this will match all the text that's inputed and any other a-z chars until it reaches a word boundry.

PoloniculMov
  • 219
  • 3
  • 9
  • Interesting answer, How can I add this regex to the controller search method `render json: names.map(&:downcase).select {|i| i.include?(params[:term].downcase)}.to_json` – SupremeA Aug 12 '16 at 15:05
  • I tried this `render json: names.map(&:downcase).select {|i| i.include?(/#{params[:term].downcase}([a-zA-Z]*)\b/)}.to_json` but I keep getting the error `no implicit conversion of Regexp into String` – SupremeA Aug 12 '16 at 15:19
  • Tried match like so `render json: names.map(&:downcase).select {|i| i.match(/#{params[:term].downcase}([a-zA-Z]*)\b/)}.to_json` but it is still giving me the entire string not just the matched word. – SupremeA Aug 12 '16 at 20:24
  • I got it! I had to do two iterations 1 to find the matching strings and a second to remove the stuff I don't want from the string! Thanks @PoloniculMov and PauloAbreu – SupremeA Aug 13 '16 at 02:35