0

I made a RegExp that functions correctly in my code, but it is only compatible with ES2018 because it uses negative lookbehinds. A library is consuming the RegExp function, so I can't change how the RegExp is used.

I've tried placing a non-capturing groups at the start to find "not" the characters, but it does not work the same when I test it.

/((?<![\^+-])[0-9]+)/g

When running my test suite regexes like /(?:[^^+-])([0-9]+)/g fail the tests because they include the preceding character.

Luca Kiebel
  • 9,790
  • 7
  • 29
  • 44
Russell
  • 99
  • 1
  • 5
  • 1
    How exactly are you implementing the pattern - what are you doing with the match(es)? Can you post the Javascript code? – CertainPerformance May 20 '19 at 12:27
  • @CertainPerformance `/((?<![\^+-])[0-9]+)/g` is used to match the element or group counts in `[Cu(NH3)6]^+6_3(H2O)^+5_12`. – Russell May 20 '19 at 12:31
  • @CertainPerformance I already checked the solutions given in [this thread](https://stackoverflow.com/questions/432493/how-do-you-access-the-matched-groups-in-a-javascript-regular-expression). [Here](https://github.com/teamtofu/mcul/blob/9150fb1b9dc5a2c28d26537b33dbf7c9c338521a/src/parsers/basic.js#L13) is the context for my code. I am feeding the code into another library, as stated, so unfortunately I cannot use the reversal method. – Russell May 20 '19 at 12:42
  • You should post all the relevant Javascript code in the question itself (like in first comment). If you can't alter how the regex is being used (and only full matches are extracted), then what you're looking for isn't possible. I'd look to change the `Token` function – CertainPerformance May 20 '19 at 12:48

1 Answers1

0

the negative lookbehind matches at the start, so:

/(?:[^^+-]|^)([0-9]+)/g
Aprillion
  • 21,510
  • 5
  • 55
  • 89
  • `'[Cu(NH3)6]^+6_3(H2O)^+5_12'.match(/(?:[^^+-]|^)([0-9]+)/g)` still captures the preceding characters. – Russell May 20 '19 at 12:35