How to extract the URL from the CSS background-image property with RegEx

Posted on September 19, 2019

A snippet of code I found on my hard-drive that might be useful, thus, I want to document here.

It's a simple Reg-exp to extract the full URL from a CSS background-image url property:

// you have
var csspath = 'url(myImage.png)'

// and want to get 'myImage.png'
var reg = new RegExp(/url\((.*)\)/gim)
var cleanPath = reg.exec(csspath)
if(cleanPath) console.log(cleanPath[1])

Have fun!

Update: A better reg-ex in this post.