Question

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?

Était-ce utile?

La solution

Use the expression [<>]=? number.

[<>] means either < or >.

=? is an optional = sign.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top