搜索插件、代码仓与 APP 分类

插件详情v0.4.0

path_to_regexp

将路径(如 '/user/:id')转换为正则表达式。

AndroidiOSWebmacOSWindowsLinux
近 30 天下载
6.9万
喜欢数
46
Pub 分数
160

安装插件

flutter pub add path_to_regexp

插件介绍

基于插件公开信息、社区数据与项目使用情况整理的选型参考。

一个 Dart 包,可将路径模式(如 '/user/:id')转换为正则表达式以进行路由匹配。它通过支持参数捕获、通配符和可选段,实现了动态路径解析。非常适合用于构建 Flutter 应用中的路由系统,提供了一种轻量级的解决方案,用于将 URL 与预定义路由进行匹配。该包可在所有主要平台上兼容,并能与现有的导航逻辑无缝集成。

适合场景

  • 动态路由匹配
  • URL 模式解析
  • 导航系统实现
  • 路由参数提取
  • 自定义路由器开发
  • API端点匹配

主要特点

  • 支持命名参数(例如 :id)
  • 处理可选段落
  • 包含通配符匹配
  • 生成可重用的 RegExp 对象
  • 轻量且快速
  • 跨平台兼容性

适合对象

  • 开发自定义路由器的开发者
  • 需要灵活 URL 路由的应用程序
  • 前端导航与动态路径
  • 解析复杂的路由模式
  • 与现有路由库集成

使用建议

  • 不处理查询参数
  • 无内置中间件支持
  • 需要手动路由解析
  • 仅限路径级别匹配
  • 不适用于完整的HTTP服务器使用

原始 README

以下为关联源码仓库的 README 原文快照,最新内容请访问源码仓库。

path_to_regexp

Pub Travis

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

Matching

pathToRegExp() converts a path specification into a regular expression that matches conforming paths.

final regExp = pathToRegExp('/user/:id');
regExp.hasMatch('/user/12'); // => true
regExp.hasMatch('/user/alice'); // => true
Custom Parameters

By default, parameters match anything until the next delimiter. This behavior can be customized by specifying a regular expression in parentheses following a parameter name.

final regExp = pathToRegExp(r'/user/:id(\d+)');
regExp.hasMatch('/user/12'); // => true
regExp.hasMatch('/user/alice'); // => false
Extracting Parameters

Parameters can be extracted from a path specification during conversion into a regular expression.

final parameters = <String>[];
final regExp = pathToRegExp('/user/:id', parameters: parameters);
parameters; // => ['id']
Extracting Arguments

extract() maps the parameters of a path specification to their corresponding arguments in a match.

final parameters = <String>[];
final regExp = pathToRegExp('/user/:id', parameters: parameters);
final match = regExp.matchAsPrefix('/user/12');
extract(parameters, match); // => {'id': '12'}

Generating

pathToFunction() converts a path specification into a function that generates matching paths.

final toPath = pathToFunction('/user/:id');
toPath({'id': '12'}); // => '/user/12'

Tokens

parse() converts a path specification into a list of tokens, which can be used to create a regular expression or path generating function.

final tokens = parse('/users/:id');
final regExp = tokensToRegExp(tokens);
final toPath = tokensToFunction(tokens);

Similar to pathToRegExp(), parameters can also be extracted during parsing.

final parameters = <String>[];
final tokens = parse('/users/:id', parameters: parameters);

If you intend to match and generate paths from the same path specification, parse() and the token-based functions should be preferred to their path-based counterparts. This is because the token-based functions can reuse the same tokens, whereas each path-based function must parse the path specification anew.

Options

Prefix Matching

By default, a regular expression created by pathToRegExp or tokensToRegExp matches the entire input. However, if the optional prefix argument is true, it may also match as a prefix until a delimiter.

final regExp = pathToRegExp('/user/:id', prefix: true);
regExp.hasMatch('/user/12/details'); // => true
Case Sensitivity

By default, a regular expression created by pathToRegExp or tokensToRegExp is case sensitive. To create a case insensitive regular expression, set caseSensitive to false.

final regExp = pathToRegExp('/user/:id', caseSensitive: false);
regExp.hasMatch('/USER/12'); // => true

Demo

Try the path_to_regexp_demo to experiment with this library.

Credit

This package is heavily inspired by its JavaScript namesake path-to-regexp.

所属分类

源码仓库

使用此插件的项目