Search code examples
javascriptregexregular-languagensregularexpression

Regex to find a unique pattern


Hi I have tried getting a specific pattern using regex but did not succeed. I have the following string and would like to know if it can be done with regex:

query pets {
  breed(some params lorem ipsum) {
    totalDogs
    dogs {
      adopted(some params) {
        has {
          owner
          ownerId
          petId
        }
      }
      ownerName
      ownerId
      Id
    }
  }
}

My final goal is to get the following string from the above string of data: adopted.has[*].ownerId

The [*] in adopted.has[*].ownerId is to be hardcoded in the regex pattern or any other way if possible.


Solution

  • If I understood you correctly, adopted, has and ownerId are variables? If so - see this: https://regex101.com/r/c7TEKR/1

    /\s+(.+)\(.+\).+\s+(.+?)\s+.+\s+.+\s+(.+)/g => $1.$2[*].$3