-1

I have string like this:

(3) Request Inbox

Now I want to detect number 3 in parentheses. Pay attention just 3. I write this regex in javascript but it doesn't work in Firefox.

var regex = new RegExp(/(?<=\()\d+(?:\.\d+)?(?=\))/g);

Error in console: SyntaxError: invalid regexp group

Demo link

Ehsan Ali
  • 1,362
  • 5
  • 25
  • 51
  • If you just want to match the number, do you even need the lookahead? What about something as simple as: `/^\((\d+)\)/g` - it matches in group 1. Or do you mean, only the full match? – fubar Nov 15 '18 at 06:22

2 Answers2

2

Positive lookbehind is not supported by most browsers so use an alternative way to get your answer.

Something like this,

var string = "somestring(12)";
var exp = /(?:\()(\d+)(?:\.\d+)?(?=\))/;
var mat = string.match(exp);

if(mat) {
    console.log(mat[1]);// second index
}

This should only give 12 as the answer

Ehsan Ali
  • 1,362
  • 5
  • 25
  • 51
Simba3696
  • 151
  • 1
  • 1
  • 8
  • it has problem, please check it [https://regex101.com/r/eNOcvx/2](https://regex101.com/r/eNOcvx/2) – Ehsan Ali Nov 15 '18 at 08:12
  • 1
    The first parenthesis is shown as 'matched' string but when you get the matched group it will come out as number only – Simba3696 Nov 15 '18 at 08:19
0

For a full match try this:

var regex = new RegExp(/(?=\d+\))\d+/g);
  • Welcome to StackOverflow! Your answer need more explanation about how the code works to be a good answer. – Tân Nov 15 '18 at 06:30