Vahi
Vahi

Reputation: 605

Multiple filter regex

Sample Data:

ID Name User 12 Test Same 14 Xyz Joe 15 Abc John 16 Def Bill 17 Ghi Donald

If a user searches for Abc or Joe, he should get that rows.

Regex:

'Abc|Joe'

Output:

14 Xyz Joe
15 Abc John

Now, if the user further searches for e, it should filter based on the previous output(2 rows retrieved), so I will just get 14 Xyz Joe . Is this possible using regex?

I am trying to have all this in one regex.

`'Abc|Joe and the second filter goes here (All in one regex)'`

Use case: The user selects checkboxes to set the filters he wants to apply on the data (All the data in the columns Name and User are available). He may then search again on the filtered result using a search textbox.

Upvotes: 1

Views: 5148

Answers (2)

Mariano
Mariano

Reputation: 6511

For the 2 filters:

/^\d+\s+(?:Abc|Xyz|Def)\s+\S*(?:Jo|ill).*/mg;

If the user doesn't specify the second filter, you could just leave it empty as (?:).


I'm positive you could create these kind of expressions if you read a couple of minutes about regex syntax, so allow me to recommend:

  1. Regular Expressions Tutorial (regular-expressions.info). A quite comprehensive tutorial to learn regex.
  2. regex101.com. Allows you to test different expressions and understand the way a pattern matches the subjet string.

Upvotes: 1

Kerwin
Kerwin

Reputation: 1212

((firstRegex)(?:.*(secondRegex)))|((secondRegex)(?:.*(firstRegex)))

((Abc|Xyz)(?:.*(Jo)))|((Jo)(?:.*(Abc|Xyz)))

See Demo

we don't know which regex would before or after,so it have two case and we use | combine these case.If have more search,suggest you write some code.

Upvotes: 1

Related Questions