Skip to content

Instantly share code, notes, and snippets.

@sgregson
Last active August 29, 2015 14:03
Show Gist options
  • Save sgregson/689b9e81b119d50be757 to your computer and use it in GitHub Desktop.
Save sgregson/689b9e81b119d50be757 to your computer and use it in GitHub Desktop.
Useful Regular Expressions

CSS/SCSS

Select CSS selector (fast but won't select selectors on line 1 because of initial lookbehind)

(?<=[\},\n])[+#:\.\w -]+(?=[\{,])

Select groups of properties

(?<=[\{,\n])(.+[^,\{]\n)+(?=[\},])

OR to get all the rules related to certain selectors:

  1. grab from the first occurrance of a selector in a rulset to the closing brace (use as scope for second filter)
    .*\.(\.CLASS|#ID|SELECTOR|SELECTOR)\b[^}]*\n\}
  2. select only the relevant selectors and the properties
    (.*(\.CLASS|#ID|SELECTOR|SELECTOR)\b[^\n{]*)|(\{(\n[^}]*\}))
  3. filter out unnecessary commas
    ,\s+\{

Find/Replace selectors containing one rule to one line

  • find: \{\s*([^\n]*)\n\s*\}
  • replace: {\1}

##CSS Coding Standards

These are not guaranteed to be perfect, but fairly reliable.
Always lint your files after performing any batch operations.

Unnecessary "px" on 0px

  • find: (?<=[^\d]0)px
  • replace: empty

Unnecessary leading zero

  • find: ([^\d])0\.(?=\d)
  • replace: \1.

Hex colors that can be reduced to their short form

  • find: (?<=#)([0-9a-fA-F])\1([0-9a-fA-F])\2([0-9a-fA-F])\3
  • replace: \1\2\3

Hex colors to lowercase

  • find: #[A-F0-9]{3,6}
  • convert case: lowercase

Opening brace has too little / too much white space

  • find: (([^\s])|\s{2,})\{
  • replace: \2 {

Function arguments missing space after comma

You'll need to run this multiple (usually 3x) times

  • find: ((?<=rgba)|(?<=gradient))\((.*?),([^\s])
  • replace: (\2, \3

Rule doesn't end with semicolon

  • find: (?<=[^;/\s\n}])\n\}
  • replace: ;\n}

Shorthand value declaration

  • find: (?<=: )(.+?) \1 \1 \1(?=;)
  • replace: \1
  • find: (?<=: )(.+?) (.+?) \1(?=;)
  • replace: \1 \2
  • find: (?<=: )(.+?) (.+?) \1 \2(?=;)
  • replace: \1 \2
  • find: (?<=: )(.+?) (.+?) (.+?) \2(?=;)
  • replace: \1 \2 \3

Git

Filter a Diff to only CSS and SCSS files:

^diff.*\.s?css\n(.*\n)+?(?=diff)

Select Merge Conflict Sections

  • yours: ((?<=<<<<<<< HEAD\n)(.*\n)*?.*?(?=\n\|\|\|\|\|\|\|))
  • ancestor: ((?<=\|\|\|\|\|\|\| merged common ancestors\n)(.*\n)*?.*?(?=\n\=\=\=\=\=\=\=))
  • theirs: ((?<========\n)(.*\n)*?.*?(?=\n>>>>>>>))

HTML/PHP

match content of image tag

<img [\d\w=\s":/\\\.@&;]*>

To create emmet-style outline:

  1. match opening/closing tag and class/id references (?<=<)/?\w+(?=[ >])|((class|id)="[-\w_\s\d]+")
  2. find class delimeters (class=")|( ) and replace with "."
  3. find all id delimeters (id=")|( ) and replace with "#"
  4. select EOL chars \.?"$ and delete
  5. select tag chars ((?<=^/)|(?<=^))\w+, and wrap with "<>" (→, >, home, <)
  6. select beginning of attr lines ^(?=[#\.]) and back-delete
  7. HTML Prettify (make sure 'a' tags are unformatted)! and convert spaces to tabs for indentation
  8. select tag content (^\s*</\w+>)|<|> and delete
  9. select empty lines ^$ and delete
  10. select ID tags #[\w#-]+[^$] and move to EOL (cut, end, paste) only works with one ID per line
  11. select equally-indented lines (^\s+).*(\n\1[^\s]+)+ andUNIQUE-ify
  12. set SCSS syntax

Other Regex

\{\n(.*\n){0,1}(.*text-align: ?right.*\n)(.*\n){0,1}\}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment