UrlMatcher
object in module ui.router.util

Description

Matches URLs against patterns and extracts named parameters from the path or the search part of the URL. A URL pattern consists of a path pattern, optionally followed by '?' and a list of search parameters. Multiple search parameter names are separated by '&'. Search parameters do not influence whether or not a URL is matched, but their values are passed through into the matched parameters returned by exec.

Path parameter placeholders can be specified using simple colon/catch-all syntax or curly brace syntax, which optionally allows a regular expression for the parameter to be specified:

  • ':' name - colon placeholder
  • '*' name - catch-all placeholder
  • '{' name '}' - curly placeholder
  • '{' name ':' regexp|type '}' - curly placeholder with regexp or type name. Should the regexp itself contain curly braces, they must be in matched pairs or escaped with a backslash.

Parameter names may contain only word characters (latin letters, digits, and underscore) and must be unique within the pattern (across both path and search parameters). For colon placeholders or curly placeholders without an explicit regexp, a path parameter matches any number of characters other than '/'. For catch-all placeholders the path parameter matches any number of characters.

Examples:

  • '/hello/' - Matches only if the path is exactly '/hello/'. There is no special treatment for trailing slashes, and patterns have to match the entire path, not just a prefix.
  • '/user/:id' - Matches '/user/bob' or '/user/1234!!!' or even '/user/' but not '/user' or '/user/bob/details'. The second path segment will be captured as the parameter 'id'.
  • '/user/{id}' - Same as the previous example, but using curly brace syntax.
  • '/user/{id:[^/]*}' - Same as the previous example.
  • '/user/{id:[0-9a-fA-F]{1,8}}' - Similar to the previous example, but only matches if the id parameter consists of 1 to 8 hex digits.
  • '/files/{path:.*}' - Matches any URL starting with '/files/' and captures the rest of the path into the parameter 'path'.
  • '/files/*path' - ditto.
  • '/calendar/{start:date}' - Matches "/calendar/2014-11-12" (because the pattern defined in the built-in date Type matches 2014-11-12) and provides a Date object in $stateParams.start

Usage

UrlMatcher(pattern, config[, parentMatcher]);

Parameters

ParamTypeDetails
patternstring

The pattern to compile into a matcher.

configObject

A configuration object hash:

parentMatcher
(optional)
Object

Used to concatenate the pattern/config onto an existing UrlMatcher

  • caseInsensitive - true if URL matching should be case insensitive, otherwise false, the default value (for backward compatibility) is false.
  • strict - false if matching against a URL with a trailing slash should be treated as equivalent to a URL without a trailing slash, the default value is true.

Returns

Object

New UrlMatcher object

Methods

Properties