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

1
  • You have at least 2 questions that you've duplicated each -- please don't do this. If your question isn't getting attention, edit it with additional details and it will be bumped back up in the queue. Posting it a second time isn't acceptable. Commented Feb 28, 2014 at 5:26

1 Answer 1

2

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)\([\"\']?(.*?)[\"\']?\)
Sign up to request clarification or add additional context in comments.

7 Comments

I dont want url to get prefixed to the image reference. I just need the value inside parenthesis. In this, I am getting url("../Images/bg_topnav.gif"). I just want ../Images/bg_topnav.gif.
you will get that only if you look at the matched groups carefully
I had this Regex (?<=url().*?(?=)), But I used to get inverted comas also with this regex. If possible, can you please edit this regex, so as to not get inverted comas, but only Image reference.
This wont work in this case, Please provide Regex to get only Image reference inside url:("").
You can use this regex to find value of background-image attribute, then after you extract url(), url(''), etc.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.