Skip to content

Instantly share code, notes, and snippets.

@anentropic
Last active June 19, 2020 12:41
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 anentropic/613564 to your computer and use it in GitHub Desktop.
Save anentropic/613564 to your computer and use it in GitHub Desktop.
package
{
public class StringTemplate
{
/**
* Mimics the python template string format, eg:
*
* "blah blah %(varname)s blah blah"
*
* then you pass in a 'dict' of varnames which get
* substituted into the string when run.
*/
public static function parse(str:String, vars:Object):String
{
var re:RegExp = /\%\((\w+)\)s/g;
var match:Object;
while((match=re.exec(str)) !== null) {
if (vars.hasOwnProperty(match[1])) {
str = str.replace(match[0], vars[match[1]]);
}
}
return str;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment