Reputation: 99
I have $('#tab1').css("background-image", "url(../CARImages/Icons/tab1.png)");
I need to extract ../CARImages/Icons/tab1.png
from this.
I may have background-image:url(../Images/Themes/tile_bg.png);
syntax in css. I need to extract ../Images/Themes/tile_bg.png
from this.
Syntax can be background:url("../Images/bg_topnav.gif"); I need to extract ../Images/bg_topnav.gif from this.
4.. Syntax can be background:url('../Images/bg_topnav.gif'). I need to extract ../Images/bg_topnav.gif from this.
Can I have same Regex for the above syntax to extract image reference.
Output should not contain inverted comas, which are present in the original syntax.
Upvotes: 1
Views: 717
Reputation: 11116
use this:
/url\([\"\']?(.*?)[\"\']?\)/
demo : http://rubular.com/r/EXrGNXnmnn
for eg :
var str = '$(\'#tab1\').css("background-image", "url(../CARImages/Icons/tab1.png)");';
var res = /url\([\"\']?(.*?)[\"\']?\)/.exec(str);
console.log(res[1]);
regex with look behind
(?<=url)\([\"\']?(.*?)[\"\']?\)
Upvotes: 2