Post by §§}Associus on Jan 14, 2010 21:19:37 GMT -8
The more I learn about regular expressions, the more I realize how much I really don't know about them...
I'm trying to create a regular expression to do the following.
Have a "list" setup in a regular expression as a pattern something like /(1|2|5|8|11|21)/ and use it to parse search for a match in a variable, let's call it mem_group.
I had thought to use an array of different regular expressions containing varying quantities of numberse to search for. Something like...
var allowed_groups = new Array;
allowed_groups[0] = "/(1|2|5|8|11|21)/ ";
allowed_groups[1] = "/(2|4|5|9|15)/";
allowed_groups [2] = "/6|8|27/";
etc.
If the number of items to search for were identical in each case, I could simply have used an array of identically sized arrays and then searched through it with do while or for loop. However, since the lists of values to search for vary in length, I do not know how to do that as I'm not aware of any Javascript support for arrays of varying length.
The mem_group variable will store a single number value. Hopefully the variable will be a string type, but as I'm assigning the value to mem_group from an option "value" out of a select/option construct, I'm not sure if it is of string or integer type.
What I want the regular expression to do is let me know if the value stored in mem_group exactly equals any one of the "values" listed in the regexp pattern that are separated by the "|" alternation characters.
Examples of desired results:
using some form of a regular expression like /(1|3|5|8|11|21)/
mem_group = 1
result of match = 1
mem_group = 3
result of match = 3
mem_group = 10
result of match = no match
(the "1" in the regexp must not "match" on the "1" that begins the value of 10 in mem_group.)
mem_group = 11
result of match = 11
mem_group = 15
result of match = no match
(again the "5" in the regexp must not match the ending "5" that ends the value of 15 in mem_group.)
In other programming languages, I might use set intersections to look for a match, but I do not know of any set operations in Javascript.
If there is a better way to do this than regular expressions, I'm open to that as well as I'm not set on a particular type of solution to my problem.
Any advice or insight would be greatly appreciated.
Edit:
I did some testing and figured out that JS does support non-identically sized arrays within an array. However, if someone does know of a way to look for a match using a regular expression in this scenario, I'd love to learn how to do it.
Thanks,
I'm trying to create a regular expression to do the following.
Have a "list" setup in a regular expression as a pattern something like /(1|2|5|8|11|21)/ and use it to parse search for a match in a variable, let's call it mem_group.
I had thought to use an array of different regular expressions containing varying quantities of numberse to search for. Something like...
var allowed_groups = new Array;
allowed_groups[0] = "/(1|2|5|8|11|21)/ ";
allowed_groups[1] = "/(2|4|5|9|15)/";
allowed_groups [2] = "/6|8|27/";
etc.
If the number of items to search for were identical in each case, I could simply have used an array of identically sized arrays and then searched through it with do while or for loop. However, since the lists of values to search for vary in length, I do not know how to do that as I'm not aware of any Javascript support for arrays of varying length.
The mem_group variable will store a single number value. Hopefully the variable will be a string type, but as I'm assigning the value to mem_group from an option "value" out of a select/option construct, I'm not sure if it is of string or integer type.
What I want the regular expression to do is let me know if the value stored in mem_group exactly equals any one of the "values" listed in the regexp pattern that are separated by the "|" alternation characters.
Examples of desired results:
using some form of a regular expression like /(1|3|5|8|11|21)/
mem_group = 1
result of match = 1
mem_group = 3
result of match = 3
mem_group = 10
result of match = no match
(the "1" in the regexp must not "match" on the "1" that begins the value of 10 in mem_group.)
mem_group = 11
result of match = 11
mem_group = 15
result of match = no match
(again the "5" in the regexp must not match the ending "5" that ends the value of 15 in mem_group.)
In other programming languages, I might use set intersections to look for a match, but I do not know of any set operations in Javascript.
If there is a better way to do this than regular expressions, I'm open to that as well as I'm not set on a particular type of solution to my problem.
Any advice or insight would be greatly appreciated.
Edit:
I did some testing and figured out that JS does support non-identically sized arrays within an array. However, if someone does know of a way to look for a match using a regular expression in this scenario, I'd love to learn how to do it.
Thanks,