Ad
Replace CSS "#" (ID) With .(Class)
I've a CSS string like
#xyz{
color:#ee2ee2;
}
.abc{
background-color:#FFFFFF;
border-color:1px solid #eee;
}
.def #xyz{
border-color:1px solid #ddd;
}
I need to replace the #xyz
(or any other ID selector) with .xyz
without changing the color: #ee2ee2
(or any other color selector).
Basic java-script code would do
I've tried some regex but it fails a few cases
cssText.replace(/#([a-zA-Z])/gm, '.$1');
Ad
Answer
This will replace #
with .
only when there is a {
somewhere after in the same line.
cssText = `
#xyz
{
color:#ee2ee2;
}
.abc{
background-color:#FFFFFF;
border-color:1px solid #eee;
}
.def #xyz{
border-color:1px solid #ddd;
}
body{background-color:#fff000;}.ig26{padding:20px;}.icts{width:200px;height:200px;padding:10px;}
`;
console.log(cssText.replace(/#([a-zA-Z])(?=[^}]*{)/g, '.$1'));
Ad
source: stackoverflow.com
Related Questions
- → How to update data attribute on Ajax complete
- → October CMS - Radio Button Ajax Click Twice in a Row Causes Content to disappear
- → Octobercms Component Unique id (Twig & Javascript)
- → Passing a JS var from AJAX response to Twig
- → Laravel {!! Form::open() !!} doesn't work within AngularJS
- → DropzoneJS & Laravel - Output form validation errors
- → Import statement and Babel
- → Uncaught TypeError: Cannot read property '__SECRET_DOM_DO_NOT_USE_OR_YOU_WILL_BE_FIRED' of undefined
- → React-router: Passing props to children
- → ListView.DataSource looping data for React Native
- → Can't test submit handler in React component
- → React + Flux - How to avoid global variable
- → Webpack, React & Babel, not rendering DOM
Ad