0

I want to write some code to parse a search string. My previous post shows my filter code thanks to Scraph. Now I need to handle the following search scenarios:

  1. age>10
  2. age>=10
  3. items!=3

The following code detects if there's a number in the string:

if(queryValue.match(/\d+/g)) {
   $log.debug("Regex detected numbers");
}

I've tested this and it works with an input like >=10. Now I need to extract the >= and apply that to my search. I can create an array or operators like so:

var operators = ['>', '>=', '<', '<=', "!="]

And then have a for loop to figure out which operator it is then extract the number portion and do the filtering. However that seems overly verbose so I'm wondering if there's a cleaner solution to my problem.

3

1 Answer 1

1

Assuming only the 5 mentioned cases above, the following will work

[><]=?|!=

Regex Demo

JS Demo (not Angular JS)

var re = /([><]=?|!=)/; 
var str = ['>=', '<=', '=', '!=', '<', '>', '!', 'abc>=234'];

str.forEach(function(number) {
  if (number.match(re))
      document.writeln(number.match(re)[1] + '<br>');
});

Angular JS Demo

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.