-3

I have following string dfed operator 11 - 145. I am trying to match string operator 11 and inside this matched string, i am trying to match string 11. Currently I successfully matched operator 11 with regex ((O|o)perator(i|I)?\s*)\d+(?=\s*(-|_)\s*\d+). As I am in javascript, I can not use lookbehinds.

Is my approach correct? Is there any way to accomplish this in regex? How can i match string 11 inside previously matched string operator 11?

Thank you

Poul Bak
  • 8,860
  • 4
  • 24
  • 47
Tornike Shavishvili
  • 1,112
  • 4
  • 16
  • 33

2 Answers2

0

You could use (mind the case insensitive flag in the demo):

operator\D+(\d+)

See a demo on regex101.com.

Jan
  • 40,932
  • 8
  • 45
  • 77
0

You can modify your regex by creating a group for the first number in the matched string:

const str = 'dfed  operator  11 -  145';
const regex = /([O|o]perator)[i|I]?\s*(\d+)*[?=\s*(-|_)\s*\d+]/;
const found = str.match(regex);

console.log(found);
console.log(found[1]); // <-- group for string
console.log(found[2]); // <-- group for number
Yosvel Quintero Arguelles
  • 17,270
  • 4
  • 37
  • 42