By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,299 Members | 1,210 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,299 IT Pros & Developers. It's quick & easy.

Odd behaviour with Regular Expressions

P: 3
http://www.evergray.com/ph7/test.html

The idea behind this script is that a user types a country in the first field, and the textarea matches countries beginning with the user input.
(e.g. user types "Un" and the script should match "United States, United Kingdom, United Arab Emirates, etc');

It partially works, however it doesn't match certain countries and I'm at lost for figuring out why. If you type "U", you get:

Uganda
United Arab Emirates
United States
Uruguay

but not Ukraine

Typing "Un" doesn't match "United Kingdom" unless you type "United "

My regular expression is this:
var testreg = eval("/~"+which+"[- a-zA-Z,\\.]*~/igm");

(which is the value from the form), and the ~ are seperators between values

the rest of the code is in the URL above.

Help would be greatly appreciated!
Cheers :)
Dec 1 '06 #1
Share this Question
Share on Google+
2 Replies


P: 23
I'm not really sure what caused the problem with the letter k. But I rewrote the code and now it seems to work. If I add g (global) to the expression the same error occurs but why is a mystery to me. Since I'm matching one country at a time I don't need the g.

Expand|Select|Wrap|Line Numbers
  1. function selectCountry(which) {
  2.     var testreg = new RegExp("^" + which, "i");
  3.  
  4.      selectedCountries=new Array();
  5.  
  6.      for (var i = 0; i < countries.length; i++) {
  7.         if (testreg.test(countries[i]))
  8.              selectedCountries[selectedCountries.length] = countries[i];
  9.     }
  10.  
  11.     if (selectedCountries.length == 0)
  12.         selectedCountries[selectedCountries.length] = "No Match";
  13.  
  14.     return selectedCountries;
  15. }
Dec 4 '06 #2

P: 3
I'm not really sure what caused the problem with the letter k. But I rewrote the code and now it seems to work. If I add g (global) to the expression the same error occurs but why is a mystery to me. Since I'm matching one country at a time I don't need the g.

Expand|Select|Wrap|Line Numbers
  1. function selectCountry(which) {
  2.     var testreg = new RegExp("^" + which, "i");
  3.  
  4.      selectedCountries=new Array();
  5.  
  6.      for (var i = 0; i < countries.length; i++) {
  7.         if (testreg.test(countries[i]))
  8.              selectedCountries[selectedCountries.length] = countries[i];
  9.     }
  10.  
  11.     if (selectedCountries.length == 0)
  12.         selectedCountries[selectedCountries.length] = "No Match";
  13.  
  14.     return selectedCountries;
  15. }
Thanks, even though the whole point of this exercise was to avoid using a loop. The problem, was, appearantly, that the global regular expression did not match overlapping expressions, and as such only returned every other string. IE if the string was *country1*country2*country3* it would return only country1 and country3. The new version of the script deals with this and works perfectly:

Expand|Select|Wrap|Line Numbers
  1. function selectCountry(which)
  2. {
  3.     var testreg = eval("/~"+which+"[- a-zA-Z,\\.]*/ig");
  4.     selectedCountries=("~"+countries.join("~")).match(testreg);
  5.     if (selectedCountries) {
  6.             selectedCountries=selectedCountries.join("").split("~");
  7.             selectedCountries.shift();
  8.         }
  9.         else
  10.             selectedCountries=new Array("No Match");
  11.  
  12.         return selectedCountries;
  13. }
  14.  
Dec 9 '06 #3

Post your reply

Sign in to post your reply or Sign up for a free account.