Search code examples
javascriptpythonregexlookbehind

Convert a look-behind regex from python to javascript


I have written a regex pattern with positive look-behind in python which matches a comma-separated list of mobile numbers. The pattern is:

^\+?[0-9]+((,?)(?(1)(\+?[0-9]+)))*$

It matches any mobile number regarding following constraints:

  1. Each phone number can start with +; any other + characters are forbidden.
  2. Each phone number can start with 00.
  3. Spaces are not acceptable; not middle spaces nor peripherals.

My pattern works fine in python, but I want to convert it to javascript version in order to use it in an angular project.

I googled about javascript look-behind regex, but I couldn't figure out how to make use of indexed look-behind in javascript; the ?(1) part.

Is there any way to convert this python regex to javascript or any other equivalent regex to match my needs?


Solution

  • Given that your regex doesn’t seem to use lookbehinds or the no-clause of the conditional, I believe this should do it:

    ^\+?[0-9]+(,\+?[0-9]+)*$