我有一个困难的正则表达式的问题。我使用这个表达式来检测绝对urls (http和https
/(url {0,}\(( {0,}'| {0,}"|))(?!http|data\:).*?\)/im我想做的基本上是用preg_replace在url前面加上一个在我的脚本中定义的路径$path。基本上,此正则表达式产生两个捕获组:
group 1: (url {0,}\(( {0,}'| {0,}"|))(?!http).*?\)
group 2: ( {0,}'| {0,}"|)如何在uri开始之前一直匹配,然后在前面加上$path?我似乎找不到正确的捕获组。
发布于 2012-06-15 03:37:13
您可以使用类似以下内容:
$re = '/\b url \s*+ \( \s*+ (?| " ([^"]*+) " | \' ([^\']*+) \' | (\S*+) ) \s*+ \) /ix';
$str = preg_replace_callback($re, function ($match) {
$url = $match[1];
// do some check on the url
if(whatever)
return $match[0]; // return without change
// do whatever you want with the URL
// return new url
return "url(\"$url\")";
}, $str);https://stackoverflow.com/questions/11039758
复制相似问题