0

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?

1

3 Answers 3

5

Try this:

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

Sign up to request clarification or add additional context in comments.

Comments

4

Like bedwyr said. :)

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

url.replace("^https?:\/\/","");

4 Comments

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 :^)
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.
These characters should be supported according to the docs: livedocs.adobe.com/flex/2/docs/00001902.html#119166
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.
1

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!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.