我有一个困难的正则表达式的问题。我有这个表达式来检测绝对网址(http和https
/(url {0,}\(( {0,}'| {0,}"|))(?!http|data\:).*?\)/im
我想要做的基本上是使用preg_replace
来为我的脚本中定义的路径$path
添加url。基本上这个正则表达式导致两个捕获组:
group 1: (url {0,}\(( {0,}'| {0,}"|))(?!http).*?\)
group 2: ( {0,}'| {0,}"|)
如何在uri开始之前一直匹配,然后用$path
作为前缀?我似乎无法使捕获组正确。
答案 0 :(得分:2)
您可以使用以下内容:
$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);