0

i want to remove some css rules from a string with regex

exactly i want to remove all rules that have ":hover" for example body:hover

so i need to remove string between "," that contain :hover in something like this string

.hovered,.showFrame .container-fluid .added-element:hover , #MR_Modal_View.showFrame .container-fluid .added-element:hover , .showFrame .container-fluid:hover, #MR_Modal_View.showFrame .container-fluid:hover, .added-element:hover, body , div.test , #MR_Modal_View_Modal .added-element:hover, .ui-resizable-resizing
0

3 Answers 3

1

If you want to use regex you can do it. My suggestion is to use a three step approach. Split the text into lines, catch the text you want, and join the strings again:

var text = ".hovered,.showFrame .container-fluid .added-element:hover , #MR_Modal_View.showFrame .container-fluid .added-element:hover ,.showFrame .container-fluid:hover,#MR_Modal_View.showFrame .container-fluid:hover,.added-element:hover, body ,div.test , #MR_Modal_View_Modal .added-element:hover, .ui-resizable-resizing";
text = text.replace(/,/g,'\n').replace(/.*:hover\s*\n*/g, "").replace(/\n/g,',');

How this regex works Look this EXAMPLE

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks . but this regext only work in this exmaple , i edited my question and put new css rules, it dosent work well
0

You don't have to use a regex for this. The following code spits your string into an array of classes, filters the ones that do not contain :hover and converts them back to a string.

var classes = ".hovered,.showFrame .container-fluid .added-element:hover, #MR_Modal_View.showFrame container-fluid .added-element:hover ,.showFrame .container-fluid:hover, MR_Modal_View.showFrame .container-fluid:hover, .added-element:hover, #MR_Modal_View_Modal added-element:hover,.ui-resizable-resizing";

var filteredClasses = classes.split(',')
   .filter(function (c) { 
     return c.indexOf(':hover') === -1; 
   })
   .join(', ');

console.log(filteredClasses) // prints ".hovered,.ui-resizable-resizing"

1 Comment

as it is a huge app , i need the best performance , so i think regex
0

It's hard to match CSS rules for different browsers (compatible problems),

I found this lib may help to remove/add/change rules:

https://github.com/cssobj/cssobj

It's render CSSOM from JS into <head>, and you can change rules directly from JS Object.

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.