parseUri 1.2: Split URLs in JavaScript
来源:互联网 发布:查海关数据 编辑:程序博客网 时间:2024/06/05 15:48
原文:http://blog.stevenlevithan.com/archives/parseuri
I've just updated parseUri. If you haven't seen the older version, parseUri is a function which splits any well-formed URI into its parts, all of which are optional. Its combination of accuracy, flexibility, and brevity is unrivaled.
Highlights:
- Comprehensively splits URIs, including splitting the query string into key/value pairs. (Enhanced)
- Two parsing modes: loose and strict. (New)
- Easy to use (returns an object, so you can do, e.g.,
parseUri(uri).anchor
). - Offers convenient, pre-concatenated components (path = directory and file; authority = userInfo, host, and port; etc.)
- Change the default names of URI parts without editing the function, by updating
parseUri.options.key
. (New) - Exceptionally lightweight (1 KB before minification or gzipping).
- Released under the MIT License.
Try the demo, but make sure to come back and read the details below.
Details:
Older versions of this function used what's now called loose parsing mode (which is still the default in this version). Loose mode deviates slightly from the official generic URI spec (RFC 3986), but by doing so allows the function to split URIs in a way that most end users would expect intuitively. However, the finer details of loose mode preclude it from properly handling relative paths which do not start from root (e.g., "../file.html" or "dir/file.html"). On the other hand, strict mode attempts to split URIs according to RFC 3986. Specifically, in loose mode, directories don't need to end with a slash (e.g., the "dir" in "/dir?query" is treated as a directory rather than a file name), and the URI can start with an authority without being preceded by "//" (which means that the "yahoo.com" in "yahoo.com/search/" is treated as the host, rather than part of the directory path).
Since I've assumed that most developers will consistently want to use one mode or the other, the parsing mode is not specified as an argument when running parseUri
, but rather as a property of the parseUri
function itself. Simply run the following line of code to switch to strict mode:
parseUri.options.strictMode = true;
From that point forward, parseUri will work in strict mode (until you turn it back off).
The code:
// parseUri 1.2.2// (c) Steven Levithan <stevenlevithan.com>// MIT Licensefunction parseUri (str) {varo = parseUri.options,m = o.parser[o.strictMode ? "strict" : "loose"].exec(str),uri = {},i = 14;while (i--) uri[o.key[i]] = m[i] || "";uri[o.q.name] = {};uri[o.key[12]].replace(o.q.parser, function ($0, $1, $2) {if ($1) uri[o.q.name][$1] = $2;});return uri;};parseUri.options = {strictMode: false,key: ["source","protocol","authority","userInfo","user","password","host","port","relative","path","directory","file","query","anchor"],q: {name: "queryKey",parser: /(?:^|&)([^&=]*)=?([^&]*)/g},parser: {strict: /^(?:([^:\/?#]+):)?(?:\/\/((?:(([^:@]*)(?::([^:@]*))?)?@)?([^:\/?#]*)(?::(\d*))?))?((((?:[^?#\/]*\/)*)([^?#]*))(?:\?([^#]*))?(?:#(.*))?)/,loose: /^(?:(?![^:@]+:[^:@\/]*@)([^:\/?#.]+):)?(?:\/\/)?((?:(([^:@]*)(?::([^:@]*))?)?@)?([^:\/?#]*)(?::(\d*))?)(((\/(?:[^?#](?![^?#\/]*\.[^?#\/.]+(?:[?#]|$)))*\/?)?([^?#\/]*))(?:\?([^#]*))?(?:#(.*))?)/}};
You can download it or run the test suite.
demo test:http://stevenlevithan.com/demo/parseuri/js/#demo- parseUri 1.2: Split URLs in JavaScript
- javascript urls
- Friendly URLs in Tapestry
- Friendly URLs in Tapestry
- Friendly URLs in Tapestry
- Enable Clean URLs in Drupal
- Clickable URLs in Android TextViews
- URLs
- URLS
- URLs
- URLs
- URLs
- URLs
- urls
- URLs
- What does appending “?v=1” to CSS and Javascript URLs in link and script tags do?
- Interesting URLs in Oracle Application 11i.
- find a job in HK(urls)
- 你可以成为你想成为的人
- poj 2886 Who Gets the Most Candies?
- 码农提高工作效率
- system("pause")
- Metaio in Unity3d 教学--- 四.再谈谈图片扫描之tracking配置文件
- parseUri 1.2: Split URLs in JavaScript
- win7 上帝模式
- 深度学习
- Linux环境进程间通信: 共享内存
- C++ sort函数用法
- 【C语言】03-第一个C程序代码分析
- 考完软件项目管理的感想
- 《深入浅出 Java Concurrency》—并发容器 ConcurrentMap
- ImageGear for .NET扫描打印等图形图像处理控件介绍使用手册