Question: I have a string

`CO12dadaCO2dafdCO345daaf`

I want to extract all occurences of CO followed by some digits `/CO(\d*)([\s\S]*)/`, up to another CO.

In this case I want to get the output:

`['CO12dada', 'CO2dafd', 'CO345daaf']`

The above regex I tried also matches the rest of the CO's at once so it doesn't work.

I could get the index of a regex for the first match using `str.search`, but I need the indexes of a regex for all occurrences.

Answer: 
```js
const string = 'CO12dadaCO2dafdCO345daaf'
const result = string.match(/(CO.*?)(?=CO|$)/g)
console.log(result)
```
Or this one:

```
CO\w+?(?=CO|$)

```

see demo here: <https://regex101.com/r/gFZomh/1>

Basically: a "non-greedy" matching of all "word characters" after "CO" followed by a lookahead demanding another "CO" or end-of-string.

If you also want to match "non-word characters", you could modify the regexp to

```
CO[\w\W]+?(?=CO|$)

```

This will also work on something like `"CO12dadaCO2da,fdCO345daaf"` to produce the matches: `["CO12dada","CO2da,fd","CO345daaf"]`.