Skip to content

Instantly share code, notes, and snippets.

@ginpei
Created October 4, 2012 03:37
Show Gist options
  • Save ginpei/3831328 to your computer and use it in GitHub Desktop.
Save ginpei/3831328 to your computer and use it in GitHub Desktop.
RegExp for templating
"<{value}{value}{value}>"
.match(/^(.*?)(?:{(\$)?(\w+)})(.*?)$/)
"<{?cond}ttt{?cond2}TTT{:cond2}FFF{/cond2}{:cond}fff{/cond}>"
.match(/^(.*?)(?:{([?#]?)(.+)}(?:(.*?)(?:{\:\3}(.*)?)?{\/\3}))(.*?)$/)
"<{?cond}ttt{?cond2}TTT{:cond2}FFF{/cond2}{:cond}fff{/cond}>"
.match(/^(.*?)(?:(\$)?(\w+)}|{([?#]?)(.+)}(?:(.*?)(?:{\:\5}(.*)?)?{\/\5}))(.*?)$/)
"<{value}{value}{value}>"
.match(/^(.*?)(?:(\$)?(\w+)}|{([?#]?)(.+)}(?:(.*?)(?:{\:\5}(.*)?)?{\/\5}))(.*?)$/)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment