3

Im currently working on calculator for complex numbers (webapp). But I have small problem with

<input type="text" pattern="((\-{0,1}[0-9]+i{0,1}|\bi|\-i)[\+\-\/*]{0,1})+" />.

I tested my pattern on: https://www.freeformatter.com/regex-tester.html - and it is working as expected - accepting only stings like: '1+2, -1+2, -i+8+2i' ets. And not finding matches in string like for example '"as"'. The problem is that in my project my input element is not rejecting strings like '"asd"'.

Is something wrong with my regex, or it is something with <input pattern="">?

  • 1
    Use `pattern="((-?[0-9]+i?|\bi|-i)[+/*-]?)+"` or with non-capturing groups - `pattern="(?:(?:-?[0-9]+i?|\bi|-i)[+/*-]?)+"`, or even better, faster since it is unrolled: [`(?:-?[0-9]+i?|-?\bi)(?:[+/*-](?:-?[0-9]+i?|-?\bi))*`](https://regex101.com/r/kKBLZq/1) – Wiktor Stribiżew Oct 10 '17 at 13:33
  • Just do not escape symbols that do not have to be escaped inside a character class when using ES6 regex and `u` modifier. – Wiktor Stribiżew Oct 10 '17 at 13:37

0 Answers0