October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsClean PCRecommendedOne scan can reveal what keeps slowing WindowsLook for cleanup and repair opportunities.Run ScanOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk2 min

Creating Custom Route Matches in Angular

Angular’s UrlMatcher handles URL patterns that standard path routes cannot express. Learn its return shape, an @username example, and key configuration rules.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Use Angular Router’s matcher route property when a URL pattern cannot be expressed clearly with ordinary path and pathMatch configuration. A custom matcher receives URL segments, the current segment group, and the route configuration; it returns the segments to consume, optional named parameters, or null when the route does not match.

When to use a custom route matcher

Start with standard route syntax for ordinary static URLs, parameters such as :id, and wildcard routes. Use a custom UrlMatcher only when the URL shape or matching rule genuinely exceeds what those patterns can express. A matcher adds code that must be maintained and runs during navigation, so intricate or costly logic can make routing harder to maintain and affect performance. See Angular’s guidance on customizing route behavior.

As an Amazon Associate I earn from qualifying purchases.

Approach Best fit Trade-off
path / pathMatch Conventional static, parameterized, or wildcard routes Simpler route configuration, but limited to the standard pattern syntax
matcher A URL structure or rule standard route syntax cannot express More expressive, but requires custom logic and ongoing maintenance

These are alternatives on a route: Angular does not allow matcher to be combined with path or pathMatch on the same route. The UrlMatcher API and Route API document this restriction.

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

How Angular evaluates a matcher

The documented function type is UrlMatcher = (segments: UrlSegment[], group: UrlSegmentGroup, route: Route) => UrlMatchResult | null. Angular supplies the URL segments being considered, their current segment group, and the route configuration.

  • Return null if the URL does not satisfy your rule. Angular can then continue evaluating route configurations.
  • For a match, return an object with consumed, an array of the UrlSegment objects the route matched.
  • Optionally include posParams, an object mapping parameter names to UrlSegment objects.

The UrlMatchResult API describes these result fields.

Example: match a username prefixed with @

Angular’s custom route matches tutorial demonstrates a matcher that accepts one URL segment beginning with @, checks the username against a pattern, and exposes the part after @ as a named username parameter. The essential matcher shape is:

import { UrlMatcher } from '@angular/router';

export const usernameMatcher: UrlMatcher = (segments) => {
  if (segments.length !== 1) {
    return null;
  }

  const match = /^@([a-zA-Z0-9]+)$/.exec(segments[0].path);
  if (!match) {
    return null;
  }

  return {
    consumed: [segments[0]],
    posParams: {
      username: segments[0].toString().replace(/^@/, '')
    }
  };
};

Configure the route with the matcher rather than a path:

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
const routes = [
  {
    matcher: usernameMatcher,
    component: ProfileComponent
  }
];

The tutorial’s complete example configures routing with provideRouter(routes, withComponentInputBinding()) and binds the extracted username value to a component input. Use that binding approach when it fits your application; otherwise, read the route parameter through your chosen Angular routing pattern.

Build and register the matcher

  1. Define a function. Type it as UrlMatcher if you want TypeScript to check its arguments and return value.
  2. Reject non-matches early. Check segment count and the relevant segment content; return null as soon as the required shape fails.
  3. Return the consumed segments. Include the matched UrlSegment objects in consumed, and add named values under posParams only when needed.
  4. Assign it to a route’s matcher field. Do not also set path or pathMatch on that route.
  5. Order routes deliberately. Put more specific configurations before general ones: Angular uses first-match-wins route ordering. See Define routes.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Keep matching logic narrow

Make the matcher answer one routing question: do these segments fit this route, and which segments or parameter values should it expose? Avoid unrelated work or complex computation in the matcher. Keeping it concise makes route behavior easier to inspect and reduces the risk that matching work slows navigation.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Windows Errors? Fix Them Before They SpreadFree repair scan
Crashes, No Sound, or Screen Glitches?Free driver scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.