To address this, you have two issues to overcome. Consider this first:
https://jsfiddle.net/Twisty/ok3945ba/12/
JavaScript
$(function() {
var myText = "Elton John - Blue Eyes,Elton John - Border Song,Elton John - Can You Feel The Love Tonight,Elton John - Candle In The Wind 1997,Elton John - Circle Of Life,Elton John - Club At The End Of The Street,Elton John - Crocodile Rock,Elton John - Cry To Heaven,Elton John - Daniel,Elton John - Don't Let The Sun Go Down On Me,Elton John - Goodbye Yellow Brick Road,Elton John - I'm Still Standing,Elton John - Nikita,Elton John - Nobody Wins,Elton John - Sacrifice,Elton John - Someone Saved My Life Tonight,Elton John - Something About The Way You Look Tonight,Elton John - Song For Guy,Elton John - Sorry Seems To Be The Hardest Word,Elton John - The Bitch Is Back,Elton John - The Last Song,Elton John - The One,Elton John - You Gotta Love Someone,Elton John - Your Song,Elvis Presley - (Now And Then There's) A Fool Such As I,Elvis Presley - (Such An) Easy Question,Elvis Presley - All Shook Up,Elvis Presley - Always On My Mind,Elvis Presley - Are You Lonesome Tonight (Laughing Version),Elvis Presley - Burning Love,Elvis Presley - Crying In The Chapel,Elvis Presley - Do The Clam,Elvis Presley - Don't Cry Daddy,Elvis Presley - I Just Can't Help Believin',Elvis Presley - In The Ghetto,Elvis Presley - Jailhouse Rock,Elvis Presley - Love Letters,Elvis Presley - Moody Blue,Elvis Presley - My Boy,Elvis Presley - My Way,Elvis Presley - One Night,Elvis Presley - Rubberneckin' (Paul Oakenfold Remix Radio Edit),Elvis Presley - Stay Away,Elvis Presley - Suspicious Minds,Elvis Presley - Suspicious Minds (1999),Elvis Presley - U.S. Male,Elvis Presley - Way Down,Elvis Presley - Wooden Heart (Muss I Denn...),Elvis Presley & JXL - A Little Less Conversation";
$("#Textbox_6175_1").autocomplete({
source: function(request, response) {
$.post("/echo/html/", {
html: myText
}, function(results) {
results = $.ui.autocomplete.filter(results.split(","), request.term);
if (results.length > 20) {
results = results.slice(0, 20);
}
response(results);
});
}
});
});
Your data source, the Text file, I am assuming cannot be modified. So you will need to use $.get()
or $.ajax()
to read the Text file and split it with \r\n
. What you see here is just for this example, yet it gives yo ua working test.
In the end, you will have an array of items.
With the above example, when you search:
- "el" - you will get 20 results
- "el " - you get one results
- "el can" - you get zero results
This is the crux of your second hurdle. You need a more complex filter to find "el" AND "can" in the dataset.
Consider the following:
https://jsfiddle.net/Twisty/ok3945ba/25/
JavaScript
$(function() {
var myText = "Elton John - Blue Eyes,Elton John - Border Song,Elton John - Can You Feel The Love Tonight,Elton John - Candle In The Wind 1997,Elton John - Circle Of Life,Elton John - Club At The End Of The Street,Elton John - Crocodile Rock,Elton John - Cry To Heaven,Elton John - Daniel,Elton John - Don't Let The Sun Go Down On Me,Elton John - Goodbye Yellow Brick Road,Elton John - I'm Still Standing,Elton John - Nikita,Elton John - Nobody Wins,Elton John - Sacrifice,Elton John - Someone Saved My Life Tonight,Elton John - Something About The Way You Look Tonight,Elton John - Song For Guy,Elton John - Sorry Seems To Be The Hardest Word,Elton John - The Bitch Is Back,Elton John - The Last Song,Elton John - The One,Elton John - You Gotta Love Someone,Elton John - Your Song,Elvis Presley - (Now And Then There's) A Fool Such As I,Elvis Presley - (Such An) Easy Question,Elvis Presley - All Shook Up,Elvis Presley - Always On My Mind,Elvis Presley - Are You Lonesome Tonight (Laughing Version),Elvis Presley - Burning Love,Elvis Presley - Crying In The Chapel,Elvis Presley - Do The Clam,Elvis Presley - Don't Cry Daddy,Elvis Presley - I Just Can't Help Believin',Elvis Presley - In The Ghetto,Elvis Presley - Jailhouse Rock,Elvis Presley - Love Letters,Elvis Presley - Moody Blue,Elvis Presley - My Boy,Elvis Presley - My Way,Elvis Presley - One Night,Elvis Presley - Rubberneckin' (Paul Oakenfold Remix Radio Edit),Elvis Presley - Stay Away,Elvis Presley - Suspicious Minds,Elvis Presley - Suspicious Minds (1999),Elvis Presley - U.S. Male,Elvis Presley - Way Down,Elvis Presley - Wooden Heart (Muss I Denn...),Elvis Presley & JXL - A Little Less Conversation";
$("#Textbox_6175_1").autocomplete({
source: function(request, response) {
$.post("/echo/html/", {
html: myText
}, function(results) {
var myData = results.split(",");
var terms = request.term.split(" ");
var myResults;
if (terms.length == 1) {
myResults = $.ui.autocomplete.filter(myData, terms[0]);
} else {
myResults = myData.filter(s => terms.some(w => s.includes(w)));
}
if (myResults.length > 20) {
myResults = myResults.slice(0, 20);
}
response(myResults);
});
}
});
});
Reference: Javascript search multiple word in array
Now you should be getting the results you are expecting when searching for "el can"
Update
Found a good example that uses Regular Expressions: https://stackoverflow.com/a/37692545/463319
Per the following, we now get expected results:
https://jsfiddle.net/Twisty/ok3945ba/43/
JavaScript
$(function() {
var myText = "Elton John - Blue Eyes,Elton John - Border Song,Elton John - Can You Feel The Love Tonight,Elton John - Candle In The Wind 1997,Elton John - Circle Of Life,Elton John - Club At The End Of The Street,Elton John - Crocodile Rock,Elton John - Cry To Heaven,Elton John - Daniel,Elton John - Don't Let The Sun Go Down On Me,Elton John - Goodbye Yellow Brick Road,Elton John - I'm Still Standing,Elton John - Nikita,Elton John - Nobody Wins,Elton John - Sacrifice,Elton John - Someone Saved My Life Tonight,Elton John - Something About The Way You Look Tonight,Elton John - Song For Guy,Elton John - Sorry Seems To Be The Hardest Word,Elton John - The Bitch Is Back,Elton John - The Last Song,Elton John - The One,Elton John - You Gotta Love Someone,Elton John - Your Song,Elvis Presley - (Now And Then There's) A Fool Such As I,Elvis Presley - (Such An) Easy Question,Elvis Presley - All Shook Up,Elvis Presley - Always On My Mind,Elvis Presley - Are You Lonesome Tonight (Laughing Version),Elvis Presley - Burning Love,Elvis Presley - Crying In The Chapel,Elvis Presley - Do The Clam,Elvis Presley - Don't Cry Daddy,Elvis Presley - I Just Can't Help Believin',Elvis Presley - In The Ghetto,Elvis Presley - Jailhouse Rock,Elvis Presley - Love Letters,Elvis Presley - Moody Blue,Elvis Presley - My Boy,Elvis Presley - My Way,Elvis Presley - One Night,Elvis Presley - Rubberneckin' (Paul Oakenfold Remix Radio Edit),Elvis Presley - Stay Away,Elvis Presley - Suspicious Minds,Elvis Presley - Suspicious Minds (1999),Elvis Presley - U.S. Male,Elvis Presley - Way Down,Elvis Presley - Wooden Heart (Muss I Denn...),Elvis Presley & JXL - A Little Less Conversation";
// Ref: https://stackoverflow.com/questions/3041320/regex-and-operator
function fullTextCompare(myWords, toMatch) {
//Replace regex reserved characters
myWords = myWords.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&');
//Split your string at spaces
var arrWords = myWords.split(" ");
//Encapsulate your words inside regex groups
arrWords = arrWords.map(function(n) {
return ["(?=.*" + n + ")"];
});
//Create a regex pattern
sRegex = new RegExp(arrWords.join("") + ".*", "im");
//Execute the regex match
return (toMatch.match(sRegex) === null ? false : true);
}
$("#Textbox_6175_1").autocomplete({
source: function(request, response) {
$.post("/echo/html/", {
html: myText
}, function(results) {
var myData = results.split(",");
var myResults;
if (request.term.indexOf(" ") == -1) {
myResults = $.ui.autocomplete.filter(myData, request.term);
} else {
myResults = [];
$.each(myData, function(j, title) {
if (fullTextCompare(request.term, title)) {
myResults.push(title);
}
});
myResults.sort();
console.log(request.term, results);
}
if (myResults.length > 20) {
myResults = myResults.slice(0, 20);
}
response(myResults);
});
}
});
});
Update 2, for TXT File
$(function() {
var textFile = "path-to-file.txt";
function fullTextCompare(myWords, toMatch) {
myWords = myWords.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&');
var arrWords = myWords.split(" ");
arrWords = arrWords.map(function(n) {
return ["(?=.*" + n + ")"];
});
sRegex = new RegExp(arrWords.join("") + ".*", "im");
return (toMatch.match(sRegex) === null ? false : true);
}
$("#Textbox_6175_1").autocomplete({
source: function(request, response) {
$.get(textFile, function(results) {
var myData = results.split("\r\n");
var myResults;
if (request.term.indexOf(" ") == -1) {
myResults = $.ui.autocomplete.filter(myData, request.term);
} else {
myResults = [];
$.each(myData, function(j, title) {
if (fullTextCompare(request.term, title)) {
myResults.push(title);
}
});
myResults.sort();
console.log(request.term, results);
}
if (myResults.length > 20) {
myResults = myResults.slice(0, 20);
}
response(myResults);
});
}
});
});