Regexp for CSS image urls

css, notepad++, regex

Solution

How about:

Find what: `url\s*\("?/?(?:[^/]+/)*?([^/]+\.(?:png|gif|jpe?g))"?\)` Replace with: `url(../images/$1)`

Problem

I have a bunch of CSS containing image urls that I want to find/replace. The CSS often has more than one url per line, and the urls can vary pretty widely: ``` .class{display:inline;} .stuff{background:green url(/dir/subdir/dir.with.dots/image.png)} a{color:blue;} .more-stuff{background:url("../updir/newdir/file_image-new.jpg") no-repeat;} ``` I want to make each url into `url(../images/<filename>.<ext>)` without the rest of the path. The closest I have come is ``` /url\s*\("?(?:.+\/)*(.*?)\.(png|gif|jpe?g)"?\)/url(../images/$1.$2)/g ``` but the `(?:.+\/)*` will select the CSS between image urls. If I add a `?` to the end of that section, I end up only replacing the first directory level. Can I do this without look(ahead|behind)s? I don't know if the regex engine supports them. Other examples I see seem to have the convenience of predictable line termination, with only one url per line.

Original source

Related problems