Reputation: 217
I'm trying with the following Regex:
[^url("](.*(?:jpg|jpeg|png|gif))
But I only need the value
inside of url("
")
Example:
url("34eb1bd00e8e0e4f59a33f654edc483a.jpg")
34eb1bd00e8e0e4f59a33f654edc483a.jpg
Upvotes: 0
Views: 72
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 scanurl\(
- literal url(
['"]?
- optional quotes([^'"\)]*)
- capture group with everything up to a quote or )
['"]?
- optional quotes\)
- literal )
.*$
- match all to end of stringUpvotes: 1
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