Question: I have written a RegExp which correctly validates a single currency, eg.`EUR` or `USD`: 

```
const ccyRegEx = new RegExp(currencies.join('|'), 'i');

```

Which produces something like `EUR|USD|JPY|GBP...` given `currencies = ["EUR", "USD", "JPY", "GBP"...]` 

How would I go about using this RegExp and the `currencies` array as input to construct a new RegExp which validates currency pairs, eg.`EURUSD`, `USDJPY`?

The `currencies` array is large and could potentially change, so I want to create this regex dynamically, not hard code all the possible permutations of currency pairs.

Same currency pairs, eg.`EUREUR` and `USDUSD` are NOT valid.  

Currency pairs in different orders are valid, eg.`EURUSD` and `USDEUR`.

Answer: 
I would say to keep it simple stupid and build an intermediate array with all possibilities, example :

```
const currenciesPair = [];
const currenciesLength = currencies.length

for (let i = 0; i < currenciesLength ; i++) {

    for (let j = 0; j < currenciesLength ; j++) {
        // avoiding pair such as EUREUR, USDUSD, etc...
        if (i !== j) {
            currenciesPair.push(currencies[i] + currencies[j]);
        }
    }
}

```

And keep your regex with this array (you can also mix currenciesPair and currencies for complete possibilities) :)
This is a rather inefficient algo though, so depending on the amount of currencies, other options will be better.

If it's doable for the dataset, I would prefer Cristian Traìna s solution.

```js
const currencies = [ "EUR", "USD", "JPY", "GBP" ];

const product_matrix = currencies // loop over all the currencies
 .map( currency => currencies // We want to join the currency with all other currencies
  .map( pair => currency !== pair // Only do the join if the currency and its pair aren't the same
   ? `${ currency }${ pair }`
   : null )
  .filter( Boolean ) // Remove all the null values. YOu coudl use reduce instead of map to skip this step.
 )
 .flat(); // Flatten the 2 dimensional array into a one dimensional one.

console.log( product_matrix );
```