Kabir
Kabir

Reputation: 99

Regex for finding value inside url in background-url

  1. I have $('#tab1').css("background-image", "url(../CARImages/Icons/tab1.png)"); I need to extract ../CARImages/Icons/tab1.png from this.

  2. 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.

  3. 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

Answers (1)

aelor
aelor

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

Related Questions