Javascript: write condition regex for multiple strings

I want to write a regex for the following strings to include all strings:

B9966
ch6258
028
chIRZ170
IRZ170

B8966A 

The OUTPUT should be as follows:

966
6258
028
170
170

966A 

Conditions :

  1. If you look at b9966, you see that its output is 966. it means If the string started with one character, the next digit of the character is not part of the output.
  2. If the string is an integer, the whole string is output.
  3. I want the output of B8966A to be like this 966A.
  4. Otherwise, if the previous three conditions do not exist, all string digits are returned as output.

I wanted to write with conditions in regex (ie: (?(?condition)then|else) ) but found that javascript does not support conditions.

For example, the following solution works fine, but I’m looking for a more professional regex:

^\d+.|(?<=^\D{1}\d)\d+.|(?<=^\D{2,})\d+.

Hi @GrH, maybe you could just say any number of digits unless preceded by a single non-digit character at the beginning, plus anything else…

/(?<!^\D)\d+.*/
2 Likes

thanks a lot.

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.