Silverboy.ir Silverboy.ir - 1 month ago 8
Javascript Question

Regex for remove rules of css in js

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

Answer

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

Comments