https://stackoverflow.com/questions/32852857/javascript-conditional-regular-expression-if-then-else Conditional regex syntax is not supported by JavaScript regex engine, but it can be worked around with a non-capturing group containing 2 alternatives: