Question: 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.

Answer: 
Change the string to 

`var colors ={Red:1,White:7,Blue:27,Green:56}`

then you can return 

`colors[sColorName]`

Without regEx I would do

`var iColorNum = parseInt(sSearchStr.split(sColorName+"|")[1],10)`
See [this post](https://stackoverflow.com/questions/432493/how-do-you-access-the-matched-groups-in-a-javascript-regex):

```
var str="Red|1|White|7|Blue|27|Green|56";
var sColorName = "Red"; 
var pat = sColorName + '\\|(\\d{1,2})\\|?';
var re = new RegExp(pat ,"g");
match = re.exec(str);

//match[0] is the full match, match[1] is the color code
document.write(match[1]);

```

or in a shorter fashion:

```
function getColor(colorName, str){
    return (new RegExp(colorName + '\\|(\\d{1,2})\\|?' ,"g")).exec(str)[1];
}

```