Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>For the sake of completeness, I got to thinking about which method I should use to do this. There are basically two ways to do this as suggested by the other answers on this page.</p> <p><strong>Note:</strong> In general, extending the built-in prototypes in JavaScript is generally not recommended. I am providing as extensions on the String prototype simply for purposes of illustration, showing different implementations of a hypothetical standard method on the <code>String</code> built-in prototype.</p> <hr> <h3>Regular Expression Based Implementation</h3> <pre><code>String.prototype.replaceAll = function(search, replacement) { var target = this; return target.replace(new RegExp(search, 'g'), replacement); }; </code></pre> <h3>Split and Join (Functional) Implementation</h3> <pre><code>String.prototype.replaceAll = function(search, replacement) { var target = this; return target.split(search).join(replacement); }; </code></pre> <hr> <p>Not knowing too much about how regular expressions work behind the scenes in terms of efficiency, I tended to lean toward the split and join implementation in the past without thinking about performance. When I did wonder which was more efficient, and by what margin, I used it as an excuse to find out.</p> <p>On my Chrome Windows&nbsp;8 machine, <strong>the regular expression based implementation is the fastest</strong>, with the <strong>split and join implementation being 53% slower</strong>. Meaning the regular expressions are twice as fast for the lorem ipsum input I used.</p> <p>Check out this <a href="http://jsben.ch/#/LFfWA" rel="noreferrer"><strong>benchmark</strong></a> running these two implementations against each other.</p> <hr> <p>As noted in the comment below by @ThomasLeduc and others, there could be an issue with the regular expression-based implementation if <code>search</code> contains certain characters which are reserved as <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions#Using_special_characters" rel="noreferrer">special characters in regular expressions</a>. The implementation assumes that the caller will escape the string beforehand or will only pass strings that are without the characters in the table in <em><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions#Using_special_characters" rel="noreferrer">Regular Expressions</a></em> (MDN).</p> <p>MDN also provides an implementation to escape our strings. It would be nice if this was also standardized as <code>RegExp.escape(str)</code>, but alas, it does not exist:</p> <pre><code>function escapeRegExp(str) { return str.replace(/[.*+?^${}()|[\]\\]/g, "\\$&amp;"); // $&amp; means the whole matched string } </code></pre> <p>We could call <code>escapeRegExp</code> within our <code>String.prototype.replaceAll</code> implementation, however, I'm not sure how much this will affect the performance (potentially even for strings for which the escape is not needed, like all alphanumeric strings).</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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. 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