str.replace(), get number of matches, or last element for substitution?
<p>I have a string that uses placeholders that I'm trying to substitute with the result of function.To give an idea:</p>

<pre><code>sometext = "%number% text text %number%"replace(/%\w+%/g, function(parm) {
       return 'customtext';
}
</code></pre>

<p>What I would like to know is there a way to get the number of matches? I need that number so I can check if the current running function is substituting the last element, and if so, return something else.Something like this in the body of function</p>

<pre><code>if(lastElement) {
   return 'something else';
}

sometext = "%number% text text %number%"replace(/%\w+%/g, function(parm) {
       return 'customtext';
</code></pre>
