开发者

regexp find match

I'm working on a regexp to find and replace all matches that don't start with url(http

relative paths are going to be converted to full absolut paths ie: url(foo/bar) > url('http://foo/bar')

match:

  • url(foo/bar)
  • url('foo/bar')

don't match:

This is what I've come up with so far, but I'm not 100% there

       $fileContents = preg_replace(
            '/url\(("|\')?(?<!(http))(.+?)("|\')?\)/i',
            'url(\'' . $glmBaseUrl . $subDir . '/$3\')',
            $fileContents
        );


Something like this should suffice:

preg_replace('/url\(\'?(?!http)([^\']+?)\'?\)/',
    "url('$glmBaseUrl$subDir\$1')", $fileContents);

The problem with yours is that you used negative lookbehind instead of negative lookahead. You should also use [^\'] instead of ., otherwise url('http://foo/bar') will match by not matching the optional ', failing the negative lookahead because it's 'http and not http and then matching the single quote with .+?.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜