views:

453

answers:

3

This one may seem basic but I don't know how to do it - anybody else?

I have a string that looks like this:

private var url:String = "http://subdomain";

What regex do I need so I can do this:

url.replace(regex,"");

and wind up with this?

trace(url); // subdomain

Or is there an even better way to do it?

+4  A: 

Try this:

url.replace("http:\/\/","");

bedwyr
You are the man!
onekidney
Glad it worked :)
bedwyr
+2  A: 

Like bedwyr said. :)

This will match only at the beginning of the string and will catch https as well:

url.replace("^https?:\/\/","");
kimsnarf
Actually, I tried this and it didn't work. I don't believe Flex supports the '^' character. I'm also not sure about 's?'. I just tried it and it didn't work at all. Flex regex support isn't quite as fully fleshed out as other languages. Did you run this code? I'd be interested to hear if it worked for you :^)
bedwyr
I haven't tested it. But if the regex flavour doesn't support these basic mechanisms it can hardly be called a regex at all. More like a plain string substitution.
kimsnarf
These characters should be supported according to the docs:http://livedocs.adobe.com/flex/2/docs/00001902.html#119166
kimsnarf
Just to clarify, Flex doesn't have RegExp support, Flex is a ActionScript component library, ActionScript has RegExp support. It is true however that ActionScript does not support all RegExp, close though. But as kimsnarf shows, those are.
Tyler Egeto
+1  A: 

ActionScript does indeed support a much richer regex repetoire than bewdwyr concluded. You just need to use an actual Regexp, not a string, as the replacement parameter. :-)

var url:String;
url = "https://foo.bar.bz/asd/asdasd?asdasd.fd";
url = url.replace(/^https?:\/\//, "");

To make this perhaps even clearer

var url:String;
var pattern:RegExp = /^https?:\/\//;
url = "https://foo.bar.bz/asd/asdasd?asdasd.fd";
url = url.replace(pattern, "");

RegExp is a first class ActionScript type.

Note that you can also use the $ char for end-of-line and use ( ) to capture substrings for later reuse. Plenty of power there!

verveguy
Wow - very informative response - thanks!
onekidney