如何创建一个 reg exp 来解析这样的 url?
所以我们有 http://127.0.0.1:4773/robot10382.flv?action=read
我们需要从中退出 protocol
, ip/adress< /code>、端口、实际
url
(此处为robot10382.flv)和actions
(此处为action=read)如何将所有这些解析为一个reg exp中的字符串变量?
So we have http://127.0.0.1:4773/robot10382.flv?action=read
we need to get out from it protocol
, ip/adress
, port, actual url
(robot10382.flv here) and actions
(action=read here) how to parse all that into string vars in one reg exp?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
令我惊讶的是 AS3 不包含正确的 URL 解析工具。简单来说,使用 RE 安全地解析 URL 并不容易。 这是一个不过这样做的例子。
I'm surprised that AS3 does not include proper URL parsing facilities. To put it simply, it is not easy to safely parse a URL using an RE. Here's an example of doing it though.
<代码>/(\w+)\:\/\/(\d+\.\d+\.\d+\.\d+)\:(\d+)\/(\w+)\?(.+)/< /code> : $1 - 协议,$2 - ip,$3 - 端口,$4 - 实际 url,$5 - 操作
还有另一种方法:
协议:
url.split('://')[0]
ip/域名 :
url.split('://')[1].split(':')[0]
(或者如果未指定端口 -url.split(' ://')[1].split('/)[0]
端口:
url.split('://')[1].split(':')[1].split('/')[0]
实际网址:
url.split('?')[0].split('/').reverse()[0]
actions :
url.split('?')[1].split('&')/*最可能的分隔符恕我直言*/
这个数组的元素也可以拼接(' =')
用于分隔变量名称和值。我知道有人认为不应该使用 splice,但我认为正确使用它会很漂亮。
/(\w+)\:\/\/(\d+\.\d+\.\d+\.\d+)\:(\d+)\/(\w+)\?(.+)/
: $1 - protocol, $2 - ip, $3 - port, $4 - actual url, $5 - actionsthere's also another way:
protocol :
url.split('://')[0]
ip/domain name :
url.split('://')[1].split(':')[0]
(or if no port specified -url.split('://')[1].split('/)[0]
port :
url.split('://')[1].split(':')[1].split('/')[0]
actual url :
url.split('?')[0].split('/').reverse()[0]
actions :
url.split('?')[1].split('&')/*the most possible separator imho*/
elements of this array can also be spliced('=')
to separate variable names and values.i know there's an opinion that
splice
shouldn't be used, but i think it's just beautiful when used properly.有时,将文件路径传递给 SWF 时,您希望在将文件传递给 AS3 类之前执行 FileExistance 检查。为此,您需要知道 URI 是文件还是 http URL 或具有特定协议(名称)的任何其他 URI。
以下代码将告诉您是否正在处理本地完整路径或相对路径。
http://younsi.blogspot.com /2009/08/as3-uri-parser-and-code-sequence-to.html
Sometimes when passing a file path to a SWF you would like to perform FileExistance check before passing the file to an AS3 class. To do so you want to know if a URI is a file or an http URL or any other URI with specific protocol (moniker).
The following code will tell you if you are dealing with a local full or relative path.
http://younsi.blogspot.com/2009/08/as3-uri-parser-and-code-sequence-to.html