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: 
You can just add `{2}` to match exactly two items of your set.

For example, `/(a|b|c){2}/` will match `ab`, `ca` but not `ad`.

You can change your dynamic regular expression like this: 

```js
const currencies = ["EUR", "USD", "JPY", "GBP"];
const singleMatch = new RegExp(currencies.join('|'));

console.log('single match - EUR:', singleMatch.test('EUR'));

const doubleMatch = new RegExp('(' + currencies.join('|') + '){2}');

console.log('double match - EUR:', doubleMatch.test('EUR'));
console.log('double match - USDEUR:', doubleMatch.test('USDEUR'));
console.log('double match - GBPEUR:', doubleMatch.test('GBPEUR'));
console.log('double match - GBPGDD:', doubleMatch.test('GBPGDD'));
```

Pay attention: It matches also `EUREUR`. To avoid this, you should use a negative look-behind or a negative look-ahead.

---

Edit
----

I integrate the @PJProudhon implementation of the negative look-ahead, as he stated in the comments' section: 

```js
const currencies = ["EUR", "USD", "JPY", "GBP"];
const doubleMatch = new RegExp('(?:(' + currencies.join('|') + ')(?!\\1)){2}');

console.log('double match - EUREUR:', doubleMatch.test('EUREUR')); //expect false
```

Thank you @PJProudhon
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 );
```