-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
  • 10,450
  • 5
  • 32
  • 57
Tornike Shavishvili
  • 1,244
  • 4
  • 16
  • 35

2 Answers2

0

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

operator\D+(\d+)

See a demo on regex101.com.

Jan
  • 42,290
  • 8
  • 54
  • 79
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
  • 18,669
  • 5
  • 37
  • 46