2

I feel like this is simple but I can't get it to work...

I am using Ui-Router 2.11 which includes optional parameters with regex expressions.

I have this url which works

'/event/{eventId:[0-9]}'

it filters out everything that isn't numeric, however it only works for 1 character of input (so only 0 through 9).

I want to replace it with an expression that checks to make sure all characters in the input are numeric so I can have any input as long as it's all numbers.

I tried using expressions from this thread

'/event/{eventId:^[0-9]+$}'

but it doesn't work. The other examples include forward slashes which I can't use(via the warning in the ui-router docs). This feels like it should be incredibly simple, what am I doing wrong?

Community
  • 1
  • 1
Matt Foxx Duncan
  • 2,074
  • 3
  • 23
  • 38

1 Answers1

6

Judging by the linked documentation, I think either one of these expressions will do the trick:

'/event/{eventId:[0-9]+}'//match 1 or more digit
'/event/{eventId:[0-9]{1,}}'//same as above, alternative notation

If possible, I would replace [0-9] with \d, though. It just looks cleaner.

Elias Van Ootegem
  • 74,482
  • 9
  • 111
  • 149
  • That worked perfectly! Why does the dollar sign trip it up though? [From what I can tell](http://stackoverflow.com/questions/5921699/what-is-the-need-for-caret-and-dollar-symbol-in-regular-expression-in-js) it's supposed to signify the end of the string -- is it because I may have more input in the URL after the parameter I'm looking for? – Matt Foxx Duncan Sep 05 '14 at 14:09
  • @MattFoxxDuncan: I'm looking through the source ATM. Your patterns are escaped like so: ` var result = string.replace(/[\\\[\]\^$*+?.()|{}]/g, "\\$&");`. It would seem that the `$` sign is actually escaped, matching a literal `$`. Likely because the parameter needn't be the end of the URI (trailing slashes, GET params or a `#` anchor should be allowed). Not sure yet, though. Either way, you're defining rules. If the rule is `\d+`, that's enough: the value must be all-numeric. Beginning and end of the string are irrelevant in that respect – Elias Van Ootegem Sep 05 '14 at 14:26