I am trying to figure out how to match a pattern in this string:
"Red|1|White|7|Blue|27|Green|56"
As you can see, the numbers can be either 1 or 2 digits long (never anything else).
So, if I had a function like this:
function GetColorNumber(sColorName){
var sSearchStr="Red|1|White|7|Blue|27|Green|56";
var sColorNum;
var sPattern;
...
return sColorNum;
}
How could I use a regular expression in this function return the sColorNum (given an sColorName)?
I've solved this problem using Javascript's indexOf
and substring
functions but can't seem to figure out the pattern for an answer using regular expressions. The solution should work for all colors, including the first and last ones.