1

I am trying to find out the numbers (only numbers should match) in a string. The numbers to be identified are only those which are surrounded by '@'

So in string '@432432@Hi@534543534@' only 432432 and 534543534 should be retrieved.

I tried a regex but it uses look behind which is not supported in javascript (esp. in firefox). Thus looking for a compatible pattern.

The one I used is like the following:-

var str = '@432432@Hi@534543534@';
var list = str.match(/(?<=@)(\d{1,100})(?=@)/g)

//list is ['432432', '534543534']

Thanks in advance.

Aditya
  • 861
  • 5
  • 8
  • Use `str.match(/@(\d+)(?=@)/g).map(function(x) {return x.substr(1);})`, or [collect group 1 values](https://stackoverflow.com/questions/432493/how-do-you-access-the-matched-groups-in-a-javascript-regular-expression) while running `RegExp#exec` within a `while` loop. – Wiktor Stribiżew Nov 26 '18 at 15:01

1 Answers1

1

You could use a positive lookahead and a capturing group instead:

@(\d+)(?=@)

Regex demo

const regex = /@(\d+)(?=@)/g;
const str = `@432432@Hi@534543534@`
let m;

while ((m = regex.exec(str)) !== null) {
  if (m.index === regex.lastIndex) {
    regex.lastIndex++;
  }
  console.log(m[1]);
}
The fourth bird
  • 154,723
  • 16
  • 55
  • 70