Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
preg_match:javascript url字符串_Javascript_Regex_Pattern Matching_Src - Fatal编程技术网

preg_match:javascript url字符串

preg_match:javascript url字符串,javascript,regex,pattern-matching,src,Javascript,Regex,Pattern Matching,Src,我试图构建一个正则表达式模式,将javascript脚本中的相对URL转换为绝对URL 示例:我想替换以下所有实例(取自js脚本): 并返回以下内容: url('http://example.com/fonts/fontawesome-webfont.eot?v=4.2.0'); 适用于HTML标记()的示例模式: 我最接近的猜测(未成功)是: 我的测试表明,这将起作用: /url\\(['\'”](?!http[s]?:\\/\/)(.+)['\\'”]\\)/ 因此,您的替换应该如下所示:

我试图构建一个正则表达式模式,将javascript脚本中的相对URL转换为绝对URL

示例:我想替换以下所有实例(取自js脚本):

并返回以下内容:

url('http://example.com/fonts/fontawesome-webfont.eot?v=4.2.0');
适用于HTML标记()的示例模式:

我最接近的猜测(未成功)是:


我的测试表明,这将起作用:
/url\\(['\'”](?!http[s]?:\\/\/)(.+)['\\'”]\\)/

因此,您的替换应该如下所示:

preg\u replace(“/url\\(['\'”)(?!http[s]?:\\/\/)(.+)['\\'”]\\)/”,”http://example.com/$1',$result)


不是regex master tho-因此,请对此持保留态度

类似的做法可能适合您:

/(?<=url\((['"]))(?!http)(?=.*?\1)/

上面的正则表达式将与
url('
)后面的位置匹配,其中引号也可以是双引号

(?<=...) # is a positive lookbehind
(?!...)  # is a negative lookahead
(?=...)  # is a positive lookahead
\1       # refers to capturing group 1, in this case either ' or "
(?我使用的解决方案(感谢AndrlRC和chris85的帮助):


$result=preg\u replace(“#”)(?您的搜索字符串中没有
=
s,或者没有
url=
格式。请尝试
(url\([“'))(?!http)
。字符类是可选字符的列表。因此
s
不需要在一个字符中,而
[''.\\\\\\]
不需要
实际的
也使管道成为可选的。您还可以捕获第一个引号类型,并对第二个引号类型使用反向引用以确保匹配。例如(如果对正则表达式封装使用单引号,如果使用双引号,``s需要加倍)谢谢,这是一个很大的帮助!@ LeNeReMelZWAAL,我希望我已经提供了足够的信息,关于这个正则表达式如何工作,你可以考虑在谷歌上查找查找:-)<代码> [S]
需要一个
s
你是想让
s
成为可选的吗?是的。你建议我如何让“s”成为可选的?使用
s?
问号将使
s
匹配零个或一个类似于
https?
的时间。此外,你的第二个和第三个捕获组是空的,替换为
http://example.com/
将产生相同的结果。
使上一个字符或组成为可选的。
$result = preg_replace($pattern,'$1http://example.com/$2$3', $result);
$pattern = "#(url\s*=\s*[\"'])(?!http)([\"'])#";
/(?<=url\((['"]))(?!http)(?=.*?\1)/
http://example.com/
(?<=...) # is a positive lookbehind
(?!...)  # is a negative lookahead
(?=...)  # is a positive lookahead
\1       # refers to capturing group 1, in this case either ' or "
$result = preg_replace("#(?<=url\((['\"]))(?!https?)#",'http://example.com/', $result);