I5v
I5v

Reputation: 13

Javascript regular expression for URL

Can someone help me with building regular expression to extract couple of URLS using regular expressions from the below string

'<a href="http://mydmncd.app.corp:8080/ag/ps?q=C~0~0~0~0~0~v2hgsds4-0Ds43Hg~94~0~~~1~0~0~~http%3a%2f%2fnghj.com" target="_blank"><img border=0   
src="mydmncd.app.png" ALT="" clickUrl="http://mydmncd.app2.corp?q=1&f=4"/></a>'

Url always starts with http://mydmncd and the remaining part may vary. I have to extract the url until I find double quotes. In the above example I have to extract http://mydmncd.app.corp:8080/ag/ps?q=C~0~0~0~0~0~v2hgsds4-0Ds43Hg~94~0~~~1~0~0~~http%3a%2f%2fnghj.com

I tried with this regex /[http://mydmncd].*"/g but it is matching the last double quotes. I have also tried /[http://mydmncd].*\s/g but no luck.

See the JSFiddle

Upvotes: 1

Views: 300

Answers (3)

Dropout
Dropout

Reputation: 13866

How about

/^(?:([A-Za-z]+):)?(\/{0,3})([0-9.\-A-Za-z]+)(?::(\d+))?(?:\/([^?#]*))?(?:\?([^#]*))?(?:#(.*))?$/

for example? Does it suit your needs?

Upvotes: 0

Romski
Romski

Reputation: 1942

I believe you want to capture it as a sub-expression which can later be referenced as the capture group. regex : /\([http://mydmncd].*\)"/g. Which can then be later referenced as \1. The wikipedia page has more to say.

Upvotes: 0

ibi0tux
ibi0tux

Reputation: 2629

The problem is that the .* also matches the ".

You should be able to replace .* by [^\"]* to match any character except ".

I don't have any way to test here, hope that can help you.

Upvotes: 1

Related Questions