[RESOLVED] Possible to search for two words with RegExp
This is not my code - but appears to be how a jQuery autocomplete can fill the array that shows when you start typing.
Code:
var matcher = new RegExp($.ui.autocomplete.escapeRegex(wesInput.val().toLowerCase()), "i");
var arraytoreturn = $.grep(wesInput.autocomplete("option", "source"), function(value) {
return matcher.test(value.label || value.value || value);
});
So - if I have
SMITH, ANN
SMITH, JOHN
SMITH, JOHN AND AMY
SMITH, JOSEPH
SMITH, JOSEPH AND ANN
And I type SMITH, JOS
I currently get
SMITH, JOSEPH
SMITH, JOSEPH AND ANN
So I guess that means that the "standard no regexp escape sequences" means "find the data with left-most matching" - GENERIC as we called it back in the mainframe days.
What could I type that would tell the MATCHER.TEST function to return these two records (if it was a simple world it would be something like SMITH*ANN)
SMITH, ANN
SMITH, JOSEPH AND ANN
Re: Possible to search for two words with RegExp
Really? A whole week goes by and no one has an any ideas?
Do I have to split up the two words myself and code some kind of two pass search?
Re: Possible to search for two words with RegExp
no idea means two thing : its impossible OR who knows
and no you cant do that as far as i know , what do you mean by two pass search anyway ? as you said this piece of code will do find the data with left-most matching and thats all but you can say when you reach to "," stop and use another box for last name search ( guess that means two pass search ) :D
Re: Possible to search for two words with RegExp
likeIf I was doing it in SQL it would be something like:
Where PersonName like 'SMITH%ANN%'
I assumed REGEXP could easily do something similar...
Re: Possible to search for two words with RegExp
Actually I just discovered that this does work
(?=.*smith)(?=.*ann)
This line of code
var matcher = new RegExp($.ui.autocomplete.escapeRegex(term), "i");
was messing up the process by ESCAPING the ('s and )'s and such.
I started with typing (SMITH|ANN) - which got escaped into \(SMITH\|ANN\) - I guess so you could literally search for those REGEX-type characters.
I changed it to var matcher = new RegExp(term, "i"); and now I can type real REGEX type search factors (if I actually wanted to).
Of course having the user type (?=.*smith)(?=.*ann) is way too complicated - but I can have them enter SMITH*ANN and I can detect the "*" character and I can change that into a real REGEX search factor.
Re: [RESOLVED] Possible to search for two words with RegExp
So - I ended up writing my own makeRegex function.
Here's the final code
Code:
$.extend($.ui.autocomplete, {
escapeRegex: function(value) {
return value.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&");
},
filter: function(array, term) {
var matcher = makeRegex(term); //new RegExp($.ui.autocomplete.escapeRegex(term), "i");
var arraytoreturn = $.grep(array, function(value) {
return matcher.test(value.label || value.value || value);
});
if (arraytoreturn.length > 200) {
arraytoreturn = [{ label: "Please be more specific - " + arraytoreturn.length + " matching entries!", value: ""}];
}
return arraytoreturn;
}
});
You can see the escapeRegex function that was in place.
The makeRegex function looks like this
Code:
function makeRegex(term) {
var arrTerms = term.split(" ");
var strTerms = "";
if (arrTerms.length > 1) {
for (var i = 0; i < arrTerms.length; i++) {
arrTerms[i] = "(?=.*" + arrTerms[i] + ")";
}
strTerms = arrTerms.join("");
} else {
strTerms = $.ui.autocomplete.escapeRegex(term);
}
var regexMatcher = new RegExp(strTerms, "i");
return regexMatcher;
}
Re: [RESOLVED] Possible to search for two words with RegExp
It might be too late now, but I was going to suggest looking at http://xregexp.com/ to see if that had anything useful.