regex tips for updating URLs within CSS using JavaScript

var content = '$content {style:url("csshover.htc");}'; //using " with link
content += "$content {background:transparent image('pic.png') no-repeat;}"; //using ' with web address
content += "$content {bg-image:url('awesome.jpg');}";

Solution:

$content {
    style:url("#");
}

$content {
    background:transparent image('#') no-repeat;
}

$content {
    bg-image:url('#');
}

How do I achieve this?

Answer №1

To obtain the URL with single quotes, replace # below with $2.

var text = '#anything {behavior:url("csshover.htc");}'; //using " in url
text += ".regleft {background:transparent url('img.png') no-repeat;}"; //using ' in url
text += "#anything {background-image:url('ok.jpg');}";
text = text.replace(/url\((['"])(.+?)\1\)/g, "url('#')");

Answer №2

For those seeking a comprehensive solution that maintains both spacing and quotations:

// Example data
var css = 'one url(two) url(\'three\') url("four")  url  (  five  )';

css = css.replace(

    // Regular Expression
    /(url\W*\(\W*['"]?\W*)(.*?)(\W*['"]?\W*\))/g,

    // Replaced string
    '$1*$2*$3'

);

console.log(css);

produces

one url(*two*) url('*three*') url("*four*")  url  (  *five*  ) 

Answer №3

For example: http://jsfiddle.net/GcsLQ/2/

var code = '#anything {behavior:url("csshover.htc");}'; //using " with url
code += "#anything {background:transparent url('img.png') no-repeat;}"; //using ' with url
code += "#anything {background-image:url('ok.jpg');}";

code = code.replace(/url\([^)]+\)/g, 'url("#")');

If you need the space formatting as well, you can do this:

For example: http://jsfiddle.net/GcsLQ/3/

var text = '#anything {behavior:url("csshover.htc");}'; //using " with url
text += "#anything {background:transparent url('img.png') no-repeat;}";
text += "#anything {background-image:url('ok.jpg');}";

text = text.replace(/url\([^)]+\)/g, 'url("#")')
    .replace(/{/g,'{\n\t')
    .replace(/}/g,'\n}\n');

UPDATE: Added quotes to the replaced URL.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Retrieve the text content and identification value from each list item

I've found myself in a frustrating situation where I have an unordered list structured like this: var liList = $("#first").find("li"); var append = ""; for(var i =0; i< liList.length; i++){ var item = $(liList); append += "<option value ...

Updated Multer version causing issues with uploading image files

After using multer middleware to upload an image, I encountered an issue where the file image was showing up as undefined. This meant that I couldn't retrieve the name or file extension of the uploaded file. I'm not sure if this is an error with ...

Enhancing Angular 4 classes with dependency injection techniques

Currently utilizing angular 4 and angular cli for my project development. I have created some classes that serve as the base for my components! However, as the constructors of these classes grow during development, I find myself in a phase where I need to ...

SVG to create a gradient mask that appears transparent

I require assistance with working on svg's. I have a "background image" with another "image" layered over it. The "image" needs to have a hole cut out of it so that the background image can shine through. I managed to achieve this by utilizing svg: ...

Transferring data between various stages of the user interface

As a newcomer to angularJs, I find myself facing an issue with two forms existing in different UI states (URLs) labeled as Step 1 and Step 2. The process requires filling up Step 1 and moving to the next step by clicking the NEXT button, which then leads t ...

I am interested in implementing a feature that restricts a particular website from being accessed more than once every 24-hour period by

I'm looking to restrict user access to a webpage to once every 24 hours. Are there any solutions besides using cookies? I could use some guidance on how to implement this. Thank you in advance for your help. ...

How can I showcase Bootstrap cards horizontally within a razor foreach iteration?

I seem to be experiencing a mental block on how to arrange bootstrap cards in a horizontal layout using a for each loop. By default, they appear vertically. Below is my razor code: @foreach (var item in Model) { var imgUrl = Url.Content("~/Conte ...

How can I incorporate the mix-blend-mode into my d3 stroke technique?

I am working with a packed circles graph that has circles with a black stroke on hover. I am interested in using the CSS property mix-blend-mode: multiply to change the circle border when hovered over. My goal is to have the stroke color be a darker shade ...

How to adjust margins and padding for the <input type='radio' /> element using CSS?

Struggling to adjust the CSS for my radio buttons as their default settings are making my layout appear asymmetrical. I am using JavaScript to generate multiple forms and injecting a lot of inline styling like style='margin:0px padding:0px' to ma ...

Tips for locking the position of a table header and allowing only the table body to scroll

I have designed a table with tbody consisting of 2 tr elements. One tr is used for hiding/showing data. I am trying to keep the Table header fixed while only scrolling the table body. However, as I am using angular js ng-repeat on the tbody, I am encounte ...

What is the right way to efficiently rotate a View in React Native?

Here is my code snippet: <View style={{borderColor:"red",borderWidth:4,flex:1,transform: [{ rotate:'90deg' }]}}> <Text>Hi</Text> </View> The result can be seen here: https://i.sstatic.net/C9ryl.jpg Unf ...

The Javascript code is failing to run following an ajax request

When I use XMLHttpRequest.send to call a php script that contains some javascript code, the javasript in the called php script does not run. The process of making the call: var formdata = new FormData(); formdata.append("uploadfilename", file); var ajax ...

Deploying Node.js on a server

So I have successfully installed Node.js on my server. However, despite researching for hours, I am still confused about the next steps. I have not been able to find a clear explanation of what I need to do next. On my local machine, I can easily run Nod ...

Using animation in CSS is limited to only one time

My goal is to create a pulse animation that triggers when a button is clicked. This image shows the animation midway: https://i.stack.imgur.com/EHDqs.png When the button is clicked, the animation plays once and then stops. However, I want the animation to ...

What are the steps to incorporate a jquery-ui checkbox into a widget?

I have encountered an issue where using a jquery-ui checkbox within a container that has the class ui-widget-content is causing a problem with the CSS rules. Specifically, the ".ui-widget-content .ui-state-hover" rule is overriding the .ui-icon-check rule, ...

Struggling to pass express.js router requests and responses to a class method

I am struggling with setting up an Express JS router. I am facing difficulty passing req and res to my class method. Not Working app.get('/', controller.index) Working app.get('/', (res,req) => controller.index(req,res) The routi ...

When using JSON.stringify(), the expected text is not being returned and instead the output

Building a form that allows users to input data for downloading, I've encountered an issue. When the file is downloaded, it shows [object Object] instead of the user's input. I attempted using JSON.Stringify() but ended up with a file containing ...

Enter a socket.IO chat room upon accessing an Express route

Encountering difficulty when attempting to connect to a socket.IO room while accessing a specific route in my Express application. The current setup is as follows: app.js var express = require('express'); var app = express(); var http = requir ...

The useState hook in Next.js with Typescript is not supported

I'm working on adding a filter to my movie list. My plan is to store all the movies in 'default' and then apply filters from there when needed. However, I encountered an error along the way: Here's a snippet of my code: const [movies, ...

Place two divs side by side with the second div filling up the remaining space on the line

Hello there, can anyone lend a hand with this problem? This is the code I have been working with: <html> <body> <div style="width=100%"> <div style="float:left; background-color:Red; height:100px">Red</div> <div st ...