Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The answer by @chris is excellent, I just want to provide an alternative solution using regular expressions that works "the other way round", i.e., not by looking for occurrences of the "placeholder versions" of all items in the properties map, but by repeatedly looking for occurrences of the placeholder itself, and substituting it with the corresponding value from the property map. This has two advantages:</p> <ol> <li>If the property map grows very large, this solution should have better performance (still to be benchmarked though).</li> <li>The placeholder and the way substitutions work can easily be modified by adjusting the regular expression and the substitution function (might not be an issue here).</li> </ol> <p>The downside is, of course, that the code is a little more complex (partly due to the fact that JavaScript lacks a nice way of substituting regular expression matches using custom functions, so that's what <code>substituteRegExp</code> is for):</p> <pre><code>function substituteRegExp(string, regexp, f) { // substitute all matches of regexp in string with the value // returned by f given a match and the corresponding group values var found; var lastIndex = 0; var result = ""; while (found = regexp.exec(string)) { var subst = f.apply(this, found); result += string.slice(lastIndex, found.index) + subst; lastIndex = found.index + found[0].length; } result += string.slice(lastIndex); return result; } function templateReplace(string, values) { // repeatedly substitute [key] placeholders in string by values[key] var placeholder = /\[([a-zA-Z0-9]+)\]/g; while (true) { var newString = substituteRegExp(string, placeholder, function(match, key) { return values[key]; }); if (newString == string) break; string = newString; } return string; } alert(templateReplace("hello [[b]] [my] [name]", { "name":"world", "my":"beautiful", "a":"[b]", "b":"c", "c":"my" })); // -&gt; "hello my beautiful world" </code></pre> <p><strong>Update</strong>: I did some little profiling to compare the two solutions (jsFiddle at <a href="http://jsfiddle.net/n8Fyv/1/" rel="nofollow">http://jsfiddle.net/n8Fyv/1/</a>, I also used Firebug). While @chris' solution is faster for small strings (no need for parsing the regular expression etc), this solution performs a lot better for large strings (in the order of thousands of characters). I did not compare for different sizes of the property map, but expect even bigger differences there.</p> <p>In theory, this solution has runtime O(<em>k</em> <em>n</em>) where <em>k</em> is the depth of nesting of placeholders and <em>n</em> is the length of the string (assuming dictionary/hash lookups need constant time), while @chris' solution is O(<em>k</em> <em>n</em> <em>m</em>) where <em>m</em> is the number of items in the property map. All of this is only relevant for large inputs, of course.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload