0

can anyone please help me with regex pattern which matches string like "Viewing 1 to x of n" where x and n are any number

Anil wagh
  • 273
  • 1
  • 14

1 Answers1

4

This pattern will match your Viewing (.*\d) to (.*\d) of (.*\d) If you are stuck on having the first number as 1 just replace the first .*\d with 1.

And you can retrieve values in matched groups (basically of values of the three numbers in the string), using details mentioned in this question.

How do you access the matched groups in a JavaScript regular expression?

Edit: Add groups as per suggestion from @Probie

Yogesh_D
  • 17,656
  • 10
  • 41
  • 55
  • I think he wants to use a variable in `x` and `n` – MasOOd.KamYab Apr 26 '18 at 05:31
  • @MasOOd.KamYab Maybe, but the way i read the title says that hes probably looking to find string that have pagination info. Viewing 1 to 2 of 5 or so on. – Yogesh_D Apr 26 '18 at 05:33
  • 2
    Might be worth adding parens around the `.*\d`s so that he can actually retrive his `x` and `n` (which is what I read the question as asking) – Probie Apr 26 '18 at 05:36