path_to_regexp 0.3.0

Dart native js
Flutter Android iOS web

Converts a path such as '/user/:id' into a regular expression.

example/main.dart

import 'package:path_to_regexp/path_to_regexp.dart';

void main() {
  // Parse a path into tokens, and extract parameters names.
  final parameters = <String>[];
  final tokens = parse(r'/user/:id(\d+)', parameters: parameters);
  print(parameters); // [id]

  // Create a regular expression from tokens.
  final regExp = tokensToRegExp(tokens);
  print(regExp.hasMatch('/user/12')); // true
  print(regExp.hasMatch('/user/alice')); // false

  // Extract parameter arguments from a match.
  final match = regExp.matchAsPrefix('/user/12');
  print(extract(parameters, match)); // {id: 12}

  // Create a path function from tokens.
  final toPath = tokensToFunction(tokens);
  print(toPath({'id': '12'})); // /user/12
}
3
likes
110
pub points
90%
popularity

Converts a path such as '/user/:id' into a regular expression.

Repository (GitHub)
View/report issues

Documentation

API reference

Uploader

leonsenft@google.com

License

MIT (LICENSE)

More

Packages that depend on path_to_regexp