-2

I would like to extract a string between a (' and ',

here is what it looks like:

('I enter in {string}',

and I want to extract

I should see the menu page
I create a custom order with create your own salad
I dismiss the dietary preferences menu tooltip

These are gherkin steps in my automation framework and I've tried using

\'[^']+'

but this also returns any imports that I have in my page classes such as '../src/etc' which I don't want.

Orbita1frame
  • 203
  • 4
  • 13

1 Answers1

1

You could use a capturing group and a generator like this:

const input = "('I enter in {string}', ('blbl',"

const regex = /\('([^']*)',/ig;

function* getResults(input, regex) {
  let match;

  while (match = regex.exec(input)) {
      yield match[1];
  }
}

const results = [ ...getResults(input, regex) ];

console.log(results);

Shorter solution:

const input = "('I enter in {string}', ('blbl',"

const regex = /\('([^']*)',/ig;

const results = [ ...input.matchAll(regex) ].map(([, x]) => x);

console.log(results);
Guerric P
  • 30,447
  • 6
  • 48
  • 86
  • How do I capture all strings matching this regex?. I am reading from a file and there are multiple strings that I need to extract. I tried let array = fileContents.match(/\('(.*)',/g); . However I am getting an array of strings such as ["('I go to the payment page',", "('I close the menu', "] – Orbita1frame Jul 26 '21 at 18:29
  • @Orbita1frame I've edited my answer with two solutions for your requirement. – Guerric P Jul 27 '21 at 09:48