0

I am having an issue where I am trying to get the image url from the string.

An example of a string would be:

@media only screen and (max-width: 47.938em) { 
  .screen__start__play-btn::after { content: " "; display: block; height: 25px; position: relative; width: 100%; background-image: url(http://localhost:8080/_client/images/mouseover-line.png); background-size: cover; background-repeat: no-repeat; }
} - as a string

Here is a snippet of my code:

var cssPropertiesObj = {
    mediaQuery: ['47.938em'],//media query array function get these results
    css: ['@media only screen and (max-width: 47.938em) { 
      .screen__start__play-btn::after { content: " "; display: block; height: 25px; position: relative; width: 100%; background-image: url(http://localhost:8080/_client/images/mouseover-line.png); background-size: cover; background-repeat: no-repeat; }
    }'] // get css of each media query
};


function removeUnwantedMedia(mediaQuery, css, arrayLength) {
    var startString = css.indexOf('url(');
    var endString = css.indexOf(')');

    if(css.indexOf('url(') === -1) {
        cssPropertiesObj.mediaQuery.splice(arrayLength, 1);
        cssPropertiesObj.css.splice(arrayLength, 1);
    } else {
        css.substring(startString, endString);
        console.log(css.substring(startString, endString));
    }
    console.log(css);
}
var i;
for(i = 0; i < cssPropertiesObj.mediaQuery.length; i++){
    removeUnwantedMedia(cssPropertiesObj.mediaQuery[i], cssPropertiesObj.css[i], i);
}

What I am expecting the cssPropertiesObj.css[0] is to be equal to 'http://localhost:8080/_client/images/mouseover-line.png' without all the css jargon.

Is this possible to do in JS?

1 Answer 1

1

You can use /background-image: url\((.*)\)\;/ regex to get background image url from string:

var matches = cssPropertiesObj.css[0].match(/background-image: url\((.*)\)\;/);
if (matches) {
  cssPropertiesObj.css[0] = matches[1]
}
Sign up to request clarification or add additional context in comments.

Comments

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.