Skip to content

Comment on Screw Hashbangs: Building the Ultimate Infinite Scroll

Comments

Pretty sure his regex is off, or at least sensitive to breaking from someone monkeying with the url. Something like /pages/01-/ will match and possibly end up triggering "fetch a page range" logic on null data.

Instead he probably wants:

/pages?/([0-9]+)(?:-([0-9]*))?/?/$

The ?: part is to ignore the capture on that -, which is only really being used to group the dash and the numbers together and make the whole group optional. The other nice part is now you have either one piece of match data (the first number) or two (first and second number) so it's both "more correct" and more programmer-friendly.

Would love to know if there's a better way to ignore capture on a group that's only there to make something conditional.

AboutSource Built by g1lg1l

Hackerly is an independent reader for Hacker News, built on the public HN API. Not affiliated with Y Combinator.