Skip to content

Instantly share code, notes, and snippets.

@pbakaus
Created February 21, 2013 15:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pbakaus/5005271 to your computer and use it in GitHub Desktop.
Save pbakaus/5005271 to your computer and use it in GitHub Desktop.
Challenge: remove the CSS keyframe animations from this!
.class{rule: prop;}@keyframes spin{0%{transform:rotate(0deg)}100%{transform:rotate(360deg)}}@keyframes spinoff{0%{transform:rotate(0deg)}100%{transform:rotate(-360deg)}}@-moz-keyframes spin{0%{-moz-transform:rotate(0deg)}100%{-moz-transform:rotate(360deg)}}@-moz-keyframes spinoff{0%{-moz-transform:rotate(0deg)}100%{-moz-transform:rotate(-360deg)}}@-webkit-keyframes spin{0%{-webkit-transform:rotate(0deg)}100%{-webkit-transform:rotate(360deg)}}#id{rule: prop;}@-webkit-keyframes spinoff{0%{-webkit-transform:rotate(0deg)}100%{-webkit-transform:rotate(-360deg)}}@-ms-keyframes spin{0%{-webkit-transform:rotate(0deg)}100%{-webkit-transform:rotate(360deg)}}@-ms-keyframes spinoff{0%{-ms-transform:rotate(0deg)}100%{-ms-transform:rotate(-360deg)}}
@ThisIsMissEm
Copy link

Use parselib: https://github.com/nzakas/parser-lib

That'll give you an AST, then you can transform back to CSS. Far more reliable than regex here, I'd think.

@cowboy
Copy link

cowboy commented Feb 21, 2013

str.replace(/.*/, '');

@scottgonzalez
Copy link

css.replace( /@[a-z-]*keyframes\s+\w+\s*\{\s*(\d+%\{[^}]+\}\s*)+\}/g, "" );

@nicolasgramlich
Copy link

@cowboy wins. Clearly. =)

@hmammana
Copy link

/@(-moz-|-webkit-|-ms-)*keyframes\s\w+{(\d%{[-\w:\w+()]+}\d+%{[\w:\w()-]+)+}}/g

@pbakaus
Copy link
Author

pbakaus commented Feb 21, 2013

@scottgonzalez wins (but hmammana one's works equally well)! Thanks all!

@scottgonzalez
Copy link

The regex explained:

  1. arbitrarily prefixed keyframes: @[a-z-]*keyframes
  2. a required space: \s+
  3. keyframe name: \w+
  4. optional space before frames: \s*
  5. opening brace for frames: \{
  6. optional space before frames: \s*
  7. at least frame (\d+%\{[^}]+\}\s*)+:
    1. percentage (technically should probably be \d{1,3} or even more strict): \d+%
    2. opening brace for rule: \{
    3. arbitrary rules comprised of anything beside a closing brace: [^}]+
    4. closing brace for rule: \}
    5. optional space between rules: \s*
  8. closing brace for frames: \}

I've never used keyframes and haven't read the spec, so my terminology for names/frames/rules may be off...

@pbakaus
Copy link
Author

pbakaus commented Feb 21, 2013

nice! the idea that I needed was the grouping and repeating of frames – I somehow tried to search for "text without }}" and then }}, but that doesn't work. Thanks again!

@ajeetku
Copy link

ajeetku commented Apr 18, 2019

Try this.....
@(-moz-|-webkit-|-ms-)*keyframes\s(.*?){([0-9%a-zA-Z,\s.]*{(.*?)})*[\s\n]*}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment