0

I'm new to javascript regular expression..

this is my problem:

http://example/uploads/files/full/images/image.png

I just want to get the uploads/files/full/images/image.png

How can I do it using regex ?

Help anyone.. thanks..

1
  • regexpal.com is a really useful site for testing out javascript regular expressions/working your way through questions like this. Commented Dec 24, 2012 at 6:40

2 Answers 2

3
var someString = "http://example/uploads/files/full/images/image.png";
var path = someString.match(/https?:\/\/example\/(.+)$/)[1];

http://jsfiddle.net/Squeegy/QwHsC/

// matches http or https
https?

// matches :// since you have to escape slashes
:\/\/

// matches the domain name
example/

// captures the path all the way to the end of the string
(.+)$
Sign up to request clarification or add additional context in comments.

Comments

3

If you want to use a more comprehensive library for this sort of thing in future (ie rather than constructing/requesting specific regexes as you need them), you could take a look at something like this:

http://blog.stevenlevithan.com/archives/parseuri

In which case,

parseUri("http://example/uploads/files/full/images/image.png").path

would return

uploads/files/full/images/image.png

2 Comments

Parsing a URL is better than regex hacking a URL, when possible.
@AlexWayne Yes I agree... but I guess you get the green tick because you answered the specific question as asked! ;-) Must admit I'd probably hack out a regex rather than go out of my way to incorporate a new lib, if it were a one-off where I knew the format of the URL wouldn't change etc...

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.