Last active
June 19, 2020 12:41
-
-
Save anentropic/613564 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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