Regex to remove all but file name from links

Posted by Moasely on Stack Overflow See other posts from Stack Overflow or by Moasely
Published on 2010-05-02T16:57:44Z Indexed on 2010/05/02 17:27 UTC
Read the original article Hit count: 165

Filed under:
|

Hi, I am trying to write a regexp that removes file paths from links and images.

href="path/path/file" to href="file"
href="/file" to href="file"
src="/path/file" to src="file"

and so on...

I thought that I had it working, but it messes up if there are two paths in the string it is working on. I think my expression is too greedy. It finds the very last file in the entire string.

This is my code that shows the expression messing up on the test input:

<script type="text/javascript" src="/javascripts/jquery.js"></script>
<script type="text/javascript">
    $(document).ready(function(){
        var s = '<a href="one/keepthis"><img src="/one/two/keep.this"></a>';
        var t = s.replace(/(src|href)=("|').*\/(.*)\2/gi,"$1=$2$3$2");
        alert(t);
    });
</script>

It gives the output:

<a href="keep.this"></a>

The correct output should be:

<a href="keepthis"><img src="keep.this"></a>

Thanks for any tips!

© Stack Overflow or respective owner

Related posts about regex

Related posts about JavaScript