开发者

flash as3 delete all characters in a url string before a certain path

I am pulling in a URL from an XML file which leads with the domain name (http://site.com/) followed by the path to a series of images (images/uploads/file.jpg).

I need to have a relative path to the files, without the domain name. Previously, I was just using t开发者_如何学编程his code:

file = myXML.project.fileURL.substring(26)

To chop off the unwanted 26 characters from the string, but since I'll be going back and forth between local, staging, and live environments, I need to find a more dynamic way of doing things - basically to get rid of everything before "images/" in the URL. How do I do this?


A regEx is the best way for sure.

Test data that I used:

        var asdf:String = "http://site.com/images/uploads/file.jpg";
        var fdsa:String = "http://www.site.com/images/uploads/file.jpg";
        var anot:String = "ftp://asfd.werqsvxc.qewoihdsv.asuvxc.fwhfrew/images/asdf/asdf.afds";

        var myregTest:RegExp = /^(\w+:\/\/)?((\w+\.){0,999})?(\w+)?\//;

        fdsa = fdsa.replace(myregTest, " ");
        asdf = asdf.replace(myregTest, " ");
        anot = anot.replace(myregTest, " ");

Output for the 3 strings I tested.

images/uploads/file.jpg
images/uploads/file.jpg
images/asdf/asdf.afds

Oh the {0,999} in the middle is so it can repeat the foo.bar.foo.bar.foo.bar etc as many times as it needs. There is a better way to let it loop but I can't remember off the top of my head.

So it is limited to 998 dot's basically. (which is absurd for a URL I know) There is a better way.

Hope this helps a little.

Edit: edited to remove the leading '/' to make it exactly like what you said you needed.


Do a combination of split, splice, join:

var s:String = "http://test.local/images/something.jpg";

var output:String = s.split('/').splice(3).join("/");

trace(output);

split: explodes your string in parts

splice: extract a sub part of the array

join:re-attach parts of the array into a new string

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜