Improved on RustyTheBoyRobot and BoltClock ♦ Reply to allow comments and media queries.
Find: ([,|\}][\s$]*)((?|\/\/.*[\s$]+|.*\/\*.*\*\/[\s$]*|@media.*\{[\s$]*|)*)([\.#]?-?[_a-zA-Z]+[_a-zA-Z0-9-]*)
Replace with: $1$2 #content $3
Edit
I was informed that the code I provided only works for PHP.
Here is an improved version that should work in Javascript and PHP
Regular expression pattern *
(^(?:\s|[^@{])*?|[},]\s*)(\/\/.*\s+|.*\/\*[^*]*\*\/\s*|@media.*{\s*|@font-face.*{\s*)*([.#]?-?[_a-zA-Z]+[_a-zA-Z0-9-]*)(?=[^}]*{)
Full example *
var outputString = inputString.replace( /(^(?:\s|[^@{])*?|[},]\s*)(\/\/.*\s+|.*\/\*[^*]*\*\/\s*|@media.*{\s*|@font-face.*{\s*)*([.#]?-?[_a-zA-Z]+[_a-zA-Z0-9-]*)(?=[^}]*{)/g, "$1$2 #content $3" );
This should also fix the @Mariano Martinez Peck problem with background: linear-gradient(to bottom, #ae9e9e, #454545); and the problem with @font-face
Ryan Worth Jun 08 '16 at 6:50 2016-06-08 06:50
source share