Ayaka
Ayaka

Reputation: 217

Get the value inside of url("")

I'm trying with the following Regex:

[^url("](.*(?:jpg|jpeg|png|gif))

But I only need the value inside of url(" ")

Example:

Upvotes: 0

Views: 72

Answers (2)

Peter Thoeny
Peter Thoeny

Reputation: 7616

Try this:

const str = 'background: #fbfbfb url("34eb1bd00e8e0e4f59a33f654edc483a.jpg");';
const regex = /^.*?url\(['"]?([^'"\)]*)['"]?\).*$/;
let result = str.replace(regex, '$1');
console.log(result);

Output:

34eb1bd00e8e0e4f59a33f654edc483a.jpg

Explanation:

  • ^ - anchor at start
  • .*? - non-greedy scan
  • url\( - literal url(
  • ['"]? - optional quotes
  • ([^'"\)]*) - capture group with everything up to a quote or )
  • ['"]? - optional quotes
  • \) - literal )
  • .*$ - match all to end of string

Upvotes: 1

Luis Angel Pena Zuniga
Luis Angel Pena Zuniga

Reputation: 190

I hope this goes acording your need's

You can use this pattern for get all between in url("") OR url(''):

(?<=url\(("|')).*?(?=("|')\))

Here is te regex test: URL

And if ypu want get all that only have '.jpg', '.jpeg', '.png', '.gif':

(?<=url\(("|')).*?\.(jpg|jpeg|png|gif)(?=("|')\))

Here is te regex test: URL Img

Upvotes: 0

Related Questions