Skip to content

Instantly share code, notes, and snippets.

@johnrees
Created April 14, 2011 15:55
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 johnrees/919780 to your computer and use it in GitHub Desktop.
Save johnrees/919780 to your computer and use it in GitHub Desktop.
// Usage:
// Create a dynamic textfield with linkage name helloworld_txt and give it an initial value of '{{hello}} {{world}}!'
// add the function substitute below to a static class or put it somewhere in your project
// call it by passing the textfield name first and an object of key values to replace second
Helpers.substitute(helloworld_txt, {hello: 'It', world: 'Works' });
// run the file, the textfield should now say "It Works!"
// Put this in a generic utilities class e.g. Helpers.as
public function substitute(t:TextField,o:Object):void
{
// take the initial textfield that contains {{variable}}
var str:String = t.text;
var replace:RegExp = /{{(\w+)}}/ig;
var result:Object = replace.exec(str);
while (result != null) {
// loop through each {{variable}} and replace with object parameter
t.text = t.text.replace(result[0],o[result[1]]);
result = replace.exec(str);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment