/ Published in: ActionScript 3
This simple construct allows you to replace parts of a string with items in a hash table (e.g. and Object).
Example:
var replacements : Object = { SPEED : "slow", ADJECTIVE : "green", VERB: "slithers" };
var template: String = "The %{SPEED} %{ADJECTIVE} fox %{VERB} over the %{ADJECTIVE} dog.";
trace( makeReplacements( template, replacements ) );
//output: The slow green fox slithers over the green dog.
Example:
var replacements : Object = { SPEED : "slow", ADJECTIVE : "green", VERB: "slithers" };
var template: String = "The %{SPEED} %{ADJECTIVE} fox %{VERB} over the %{ADJECTIVE} dog.";
trace( makeReplacements( template, replacements ) );
//output: The slow green fox slithers over the green dog.
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
function makeReplacements( template : String, replacements : Object ) : String { var regExp : RegExp = new RegExp( "(%\{(.*?)})","" ); var match : Array; while( match = regExp.exec( template ) ) template = template.replace( regExp, replacements[ match[ 2 ] ] ); return template; }