Javascript 在获取背景图像URL时,如何让JQuery替换字符串的非URL部分

Javascript 在获取背景图像URL时,如何让JQuery替换字符串的非URL部分,javascript,jquery,regex,Javascript,Jquery,Regex,我有这段代码,它提取了背景图像的元素,但是它也提取了代码的“url”部分。我只需要实际的图像地址 我曾尝试使用replace函数,但在检查输出时它对字符串没有任何作用 var jsonData = { "image": jQuery('#block-content').css('background-image').replace(/^url\(['"](.+)['"]\)/, '$1'), var script = document.createElement('script'); scrip

我有这段代码,它提取了背景图像的元素,但是它也提取了代码的“url”部分。我只需要实际的图像地址

我曾尝试使用replace函数,但在检查输出时它对字符串没有任何作用

var jsonData =
{ "image": jQuery('#block-content').css('background-image').replace(/^url\(['"](.+)['"]\)/, '$1'),
var script = document.createElement('script');
script.type = 'application/ld+json';
script.text = JSON.stringify(jsonData);
jQuery("head").append(script);
元素中的图像是//imageaddress.jpg,因此它将其提取为

https://example.com/recipes/url(https://example.com/sites/image.jpg?itok=jfzYQHZx)
我怎样才能解决这个问题,让它给我

https://example.com/sites/image.jpg?itok=jfzYQHZx

用此替换图像属性

 "image": jQuery('#block-content').css('background-image').replace('url(','').replace(')','').replace(/\"/gi, ""),
希望这对你有帮助

var tmp='1〕https://example.com/recipes/url(https://example.com/sites/image.jpg?itok=jfzYQHZx)';
tmp=tmp.子串(tmp.indexOf(“(h”)+1);
tmp=tmp.substring(0,tmp.length-1);

console.log(tmp);
注意,有些URL并不总是在引号中:

url(https://example.com/sites/image.jpg?itok=jfzYQHZx)
然后添加
['”]?
=>
检测零或1个字符

var str=“url('https://example.com/sites/image.jpg?itok=jfzYQHZx“)”;//示例字符串
var str2=“url(https://example.com/sites/image.jpg?itok=jfzYQHZx)“;//不带引号
str=str.replace(//^url\([“']]?(.+?)[“']?\)/,“$1”);//替换为第一组(.+?)
str2=str2.replace(/^url\([“']”号?(.+?)[“']”号?\)/,“$1”);
console.log(str);

console.log(str2);
+
匹配除换行符以外的任何字符,替换为
[^()]+
以避免匹配括号。更好的方法是,使用
[^()”]+
您可以发布
jQuery('.\35;块内容').css('background-image')
的内容吗