title: Template string from regex
body: <p>Let's say that I have the following URL path:</p>
  
  <pre><code>/api/resource/:id
  </code></pre>
  
  <p>Given the context object <code>{id: 1}</code> the path will be transformed to <code>/api/resource/1</code>.</p>
  
  <p>Now I want to build a more complex path template:</p>
  
  <pre><code>/api/resource/:id(/relationships/:name)?
  </code></pre>
  
  <p>Here the <code>relationships</code> member is optional so that the both contexts can match:</p>
  
  <ul>
  <li><code>{id: 1, name: 'rel1'}</code> will build the string <code>/api/resource/1/relationships/rel1</code>.</li>
  <li><code>{id: 1}</code> should still build the string <code>/api/resource/1</code>.</li>
  </ul>
  
  <p>I would like to be able to build such strings using a limited set of the regular expressions magic.</p>
  
  <p>How to implement such a template engine?</p>