justin
justin

Reputation: 1699

Using Javascript RegEx

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..

Upvotes: 0

Views: 59

Answers (2)

Alex Wayne
Alex Wayne

Reputation: 187024

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
(.+)$

Upvotes: 3

Sepster
Sepster

Reputation: 4849

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

Upvotes: 3

Related Questions