I want to catch if the url is a blog page or not. A blog page can be:
/blog/blog/page/1/blog/page/143
So, page/pageNumber is optional. How can I create a regular expression that being tested returns true if the url is a blog page?
I created the regex for urls containing /page/ string and it seems to work fine:
/\/blog\/page\/[1-9]([0-9]*)/
Example:
> /\/blog\/page\/[1-9]([0-9]*)/.test("/blog/page/1")
true
> /\/blog\/page\/[1-9]([0-9]*)/.test("/blog/page/0")
false
> /\/blog\/page\/[1-9]([0-9]*)/.test("/blog/page/30")
true
But how can I set /page/number to be optional?
I tried:
/\/blog(\/page\/[1-9]([0-9]*))?/
This returns true for incorrect urls:
> /\/blog(\/page\/[1-9]([0-9]*))?/.test("/blog/p")
true
/blog/1-my-interesting-postis not the same with/blog/page/1.