【发布时间】:2020-02-20 14:23:44
【问题描述】:
我有这段代码可以提取背景图像的元素,但是它也提取了代码的“url”部分。我只需要实际的图片地址
我尝试使用替换功能,但是当我检查输出时它对字符串没有任何作用。
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
【问题讨论】:
-
.+匹配除换行符以外的任何字符,替换为[^()]+以避免匹配括号。更好,使用[^()"']+ -
能把
jQuery('#block-content').css('background-image')的内容发一下吗 -
看起来像
<div class="intro-image media_27796" style="background-image: url('//example.com/sites/g/files/awvsbl221/files/styles/intro_homepage/public/image.jpg?h=c9aba1c0&amp;itok=siFmAXu7');"> &nbsp; </div>
标签: javascript jquery regex