A bit of Regular Expression

Posted on September 26, 2018

Nothing fancy but, as always, knowing a bit of Regular Expression helps a lot sometimes.

Today has been: how to grab the path from within the url() of a background-image in a CSS rule:

// my string
var str = 'url(/uploads/VBDtr3S5WAolwjD.jpg)'

// the RegEx
var re = new RegExp('url\\((.*)\\)', 'gm')
var res = re.exec(str)

console.log(res[1]) // '/uploads/13ji57/VBDtr3S5WAolwjD.jpg'