|
-
May 21st, 2013, 08:40 AM
#1
[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
-
May 29th, 2013, 06:23 AM
#2
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?
-
May 29th, 2013, 12:47 PM
#3
Addicted Member
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 )
Body Language tells the truth! even from the grave tsaeb eht morf gninnur ,nwod deaH
All the big things started from little! teef my tsap evom sekans ,duol raor slluB
Lietome.ir
-
May 29th, 2013, 02:57 PM
#4
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...
-
May 29th, 2013, 03:33 PM
#5
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.
Last edited by szlamany; May 29th, 2013 at 03:37 PM.
-
May 29th, 2013, 04:45 PM
#6
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;
}
-
May 29th, 2013, 06:56 PM
#7
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|