Replace relative urls to absolute
Posted
by
Rocky Singh
on Stack Overflow
See other posts from Stack Overflow
or by Rocky Singh
Published on 2012-09-03T20:21:34Z
Indexed on
2012/09/03
21:38 UTC
Read the original article
Hit count: 250
I have the html source of a page in a form of string with me:
<html>
<head>
<link rel="stylesheet" type="text/css" href="/css/all.css" />
</head>
<body>
<a href="/test.aspx">Test</a>
<a href="http://mysite.com">Test</a>
<img src="/images/test.jpg"/>
<img src="http://mysite.com/images/test.jpg"/>
</body>
</html>
I want to convert all the relative paths to absolute. I want the output be:
<html>
<head>
<link rel="stylesheet" type="text/css" href="http://mysite.com/css/all.css" />
</head>
<body>
<a href="http://mysite.com/test.aspx">Test</a>
<a href="http://mysite.com">Test</a>
<img src="http://mysite.com/images/test.jpg"/>
<img src="http://mysite.com/images/test.jpg"/>
</body>
</html>
Note: I want only the relative paths to be converted to absolute ones in that string. The absolute ones which are already in that string should not be touched, they are fine to me as they are already absolute. Can this be done by regex or other means?
© Stack Overflow or respective owner