eguneys
eguneys

Reputation: 6396

How to get matched index of a regex for all occurences

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.

Upvotes: 1

Views: 471

Answers (4)

Ryszard Czech
Ryszard Czech

Reputation: 18611

Just get your matches with .split():

console.log("CO12dadaCO2dafdCO345daaf".split(/(?!^)(?=CO)/))

Result:

[
  "CO12dada",
  "CO2dafd",
  "CO345daaf"
]

(?!^)(?=CO) = matches the empty string before CO substring, but not at the string start.

Upvotes: 1

The fourth bird
The fourth bird

Reputation: 163217

Using Javascript, you can use

CO[^]*?(?=CO|$)
  • CO[^]*? Match CO, then any char including newlines as least as possible
  • (?=CO|$) Positive lookahead, assert what is on the right is either CO or the end of the string

REgex demo

Upvotes: 0

Carsten Massmann
Carsten Massmann

Reputation: 28196

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"].

Upvotes: 0

stranded
stranded

Reputation: 322

const string = 'CO12dadaCO2dafdCO345daaf'
const result = string.match(/(CO.*?)(?=CO|$)/g)
console.log(result)

Upvotes: 1

Related Questions