5

I have the following regular expression:

[^0-9+-]|(?<=.)[+-]

This regex matches either a non-digit and not + and - or +/- preceded by something. However, positive lookbehind isn't supported in JavaScript regex. How can I make it work?

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
Nicolas
  • 155
  • 1
  • 2
  • 12
  • http://stackoverflow.com/a/3569116/656243 – Lynn Crumbling Apr 27 '17 at 12:41
  • 1
    I read each answer in [Positive look behind in JavaScript regular expression](http://stackoverflow.com/questions/3569104/) and there is nothing similar to the current issue. Hence, reopened and edited to relieve any ambiguity. – Wiktor Stribiżew Apr 27 '17 at 23:44

1 Answers1

4

The (?<=.) lookbehind just makes sure the subsequent pattern is not located at the start of the string. In JS, it is easy to do with (?!^) lookahead:

[^0-9+-]|(?!^)[+-]
         ^^^^^ 

See the regex demo (cf. the original regex demo).

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563