[英]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). 我从一个XML文件中提取一个URL,该文件带有域名(http://site.com/),后面是一系列图像的路径(images / uploads / file.jpg)。
I need to have a relative path to the files, without the domain name. 我需要有一个文件的相对路径,没有域名。 Previously, I was just using this 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. 要从字符串中删除不需要的26个字符,但由于我将在本地,分段和实时环境之间来回切换,我需要找到一种更加动态的做事方式 - 基本上是为了摆脱一切之前“图片/“在网址中。 How do I do this?
我该怎么做呢?
A regEx is the best way for sure. regEx是最好的方式。
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. 我测试的3个字符串的输出。
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. 哦,中间的{0,999}是这样它可以根据需要多次重复foo.bar.foo.bar.foo.bar等。 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. 所以基本上限于998点。 (which is absurd for a URL I know) There is a better way.
(这对于我知道的URL来说是荒谬的)有一种更好的方法。
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 split : 分裂你的字符串
splice : extract a sub part of the array splice :提取数组的子部分
join :re-attach parts of the array into a new string join :将数组的部分重新附加到一个新字符串中
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.