Skip to content

Instantly share code, notes, and snippets.

@TravisMullen
Created July 23, 2016 15:56
Show Gist options
  • Save TravisMullen/d91a47c0786ea707cde0b5172b255ec3 to your computer and use it in GitHub Desktop.
Save TravisMullen/d91a47c0786ea707cde0b5172b255ec3 to your computer and use it in GitHub Desktop.
Match by Exterior Regex
// Try
/{(.*?)}/
// That means, match any character between { and }, but don't be greedy - match the shortest string which ends with } (the ? stops * being greedy). The parentheses let you extract the matched portion.
// Another way would be
/{([^}]*)}/
// This matches any character except a } char (another way of not being greedy)
/\{([^}]+)\}/
// / - delimiter
// \{ - opening literal brace escaped because it is a special character used for quantifiers eg {2,3}
// ( - start capturing
// [^}] - character class consisting of
// ^ - not
// } - a closing brace (no escaping necessary because special characters in a character class are different)
// + - one or more of the character class
// ) - end capturing
// \} - the closing literal brace
// / - delimiter
// This one works in Textmate and it matches everything in a CSS file between the curly brackets.
\{(\s*?.*?)*?\}
// selector {.
// .
// matches here
// including white space.
// .
// .}
// If you want to further be able to return the content, then wrap it all in one more set of parentheses like so:
\{((\s*?.*?)*?)\}
// and you can access the contents via $1.
// This also works for functions, but I haven't tested it with nested curly brackets.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment