문제

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?

도움이 되었습니까?

해결책

Use the expression [<>]=? number.

[<>] means either < or >.

=? is an optional = sign.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top