搜尋套件、儲存庫與 APP 分類

套件詳情v0.4.0

path_to_regexp

將路徑(例如 '/user/:id')轉換為正則表達式。

AndroidiOSWebmacOSWindowsLinux
近 30 天下載
7.3萬
喜歡數
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.

所屬分類

原始碼儲存庫

使用此套件的專案