1

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

1
  • I assume that you are not actually dealing with strings here, but with <a> elements somewhere in your document? Commented May 9, 2013 at 11:34

3 Answers 3

1

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.

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

1 Comment

I am sorry, I couldn't vote. SO says I need min 15 reputation to vote :(
0

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.

1 Comment

here is what I am trying to do. Match the url and replace it with another url. But my regex is matching till last double quote instead of first double quote
0

How about

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

for example? Does it suit your needs?

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.