473,322 Members | 1,510 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

Odd behaviour with Regular Expressions

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
2 1211
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
Vyath
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

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

Similar topics

1
by: Kenneth McDonald | last post by:
I'm working on the 0.8 release of my 'rex' module, and would appreciate feedback, suggestions, and criticism as I work towards finalizing the API and feature sets. rex is a module intended to make...
5
by: Patty O'Dors | last post by:
Can anyone help me with understanding the behaviour of a Regular Expression which is said to be like that of a stack? It says in the docs... "(?<group1-group2>) Balancing group definition....
2
by: Sehboo | last post by:
Hi, I have several regular expressions that I need to run against documents. Is it possible to combine several expressions in one expression in Regex object. So that it is faster, or will I...
4
by: Együd Csaba | last post by:
Hi All, I'd like to "compress" the following two filter expressions into one - assuming that it makes sense regarding query execution performance. .... where (adate LIKE "2004.01.10 __:30" or...
7
by: Billa | last post by:
Hi, I am replaceing a big string using different regular expressions (see some example at the end of the message). The problem is whenever I apply a "replace" it makes a new copy of string and I...
3
by: a | last post by:
I'm a newbie needing to use some Regular Expressions in PHP. Can I safely use the results of my tests using 'The Regex Coach' (http://www.weitz.de/regex-coach/index.html) Are the Regular...
25
by: Mike | last post by:
I have a regular expression (^(.+)(?=\s*).*\1 ) that results in matches. I would like to get what the actual regular expression is. In other words, when I apply ^(.+)(?=\s*).*\1 to " HEART...
1
by: Allan Ebdrup | last post by:
I have a dynamic list of regular expressions, the expressions don't change very often but they can change. And I have a single string that I want to match the regular expressions against and find...
13
by: Wiseman | last post by:
I'm kind of disappointed with the re regular expressions module. In particular, the lack of support for recursion ( (?R) or (?n) ) is a major drawback to me. There are so many great things that can...
12
by: FAQEditor | last post by:
Anybody have any URL's to tutorials and/or references for Regular Expressions? The four I have so far are: http://docs.sun.com/source/816-6408-10/regexp.htm...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.