Rella
Rella

Reputation: 66935

How to create a reg exp to parse such url?

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?

Upvotes: 0

Views: 693

Answers (3)

Thomas J Younsi
Thomas J Younsi

Reputation: 61

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

Upvotes: 0

www0z0k
www0z0k

Reputation: 4434

/(\w+)\:\/\/(\d+\.\d+\.\d+\.\d+)\:(\d+)\/(\w+)\?(.+)/ : $1 - protocol, $2 - ip, $3 - port, $4 - actual url, $5 - actions

there'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.

Upvotes: 2

D.Shawley
D.Shawley

Reputation: 59563

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.

Upvotes: 3

Related Questions