URL replacing regex

Posted by perf on Stack Overflow See other posts from Stack Overflow or by perf
Published on 2010-03-14T15:55:25Z Indexed on 2010/03/14 16:15 UTC
Read the original article Hit count: 106

Filed under:
|

I'm trying to make an url that adds a / to all hrefs and srcs in a string. It should only add a / to urls that don't have a http:// at their beginning and that don't have / yet also.

If we have this:

<a href="ABC">...
<img src="DEFG">...
<a href="/HIJ">...
<a href="http://KLMN">...

The results should be something like this:

<a href="/ABC">...
<img src="/DEFG">...
<a href="/HIJ">...
<a href="http://KLMN">...

This is what i've come up till now:

&(href|src)="?!(\/|http::\/\/)(.+)"

And the replace would be

$1="/$2"

It isn't working, though.

  • What am I doing wrong?
  • How would the working regex have to look like

© Stack Overflow or respective owner

Related posts about php

Related posts about regex