Frage

I have the following JS var:

var tokens = str.match(/\[|\]|< number|\)|and |(?:(?!and )[^\[\]])+/g);

tokens is later used in a switch() statement.

This matches occurrences of:

  • [
  • ]
  • < number
  • )
  • and (with a space at the end)

However, I would like to precede number not only by < but possibly also by other operators. The following are needed:

  • >
  • >=
  • <
  • <=

So match also has to look for < number, <= number, > number, >= number. Is there a quick and easy way to do this by, for instance, defining a wildcard/variable for the possible operators?

War es hilfreich?

Lösung

Use the expression [<>]=? number.

[<>] means either < or >.

=? is an optional = sign.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top