bflemi3
bflemi3

Reputation: 6790

Regex: Extract parts of url before and after last period

I have a url (http://www.something.com/subdirectory/image.jpg) and I would like to split the url into two parts using regex:

1) Capture first part of url before the last period (http://www.something.com/subdirectory/image)

2) Capture the last part of url after the last period (jpg)

The file extension will not always be jpg.

I have come up with something so far but it is capturing the string before the first period instead of the last:

/([^.]*)\.(.*)/

EDIT: Here's what I'm trying to do. Replace the current src by adding "_On" to the end of the image name.

$(this).attr("src").replace(/(.*)\.([^.]+)$/, "$1_On.$2")

Thanks in advance for any and all help, B

Upvotes: 1

Views: 3419

Answers (2)

veiset
veiset

Reputation: 2003

You can match all the characters thats not a period, until it reaches the end of the line:

/(.*)\.([^.]+)$/

Upvotes: 2

hsz
hsz

Reputation: 152216

Try with

/(.*)\.(.+)/

I think it'll works.

Upvotes: 1

Related Questions