Monday, May 30, 2022
HomeWordPress DevelopmentJavaScript String replaceAll

JavaScript String replaceAll


Changing a substring of textual content inside a bigger string has all the time been deceptive in JavaScript. I wrote Exchange All Occurrences of a String in JavaScript years in the past and it is nonetheless one among my most learn articles.

The confusion lies in that substitute solely replaces the primary prevalence of a substring, not all occurrences. For instance:

'yayayayayaya'.substitute('ya', 'na');
// nayayayayaya

To interchange all situations of a substring, you’ve got wanted to make use of an everyday expression:

'yayayayayaya'.substitute(/ya/g, 'na');
// nananananana

Utilizing common expressions is actually highly effective however let’s be sincere — oftentimes we merely need to substitute all situations of a easy substring that should not require an everyday expression.

Fortunately, this 12 months the JavaScript language offered us with String.prototype.replaceAll, a technique for changing with out utilizing common expressions:

'yayayayayaya'.replaceAll('ya', 'na');
// nananananana

Generally an API exists in a complicated format and requirements our bodies merely want to enhance the scenario. I am glad they did so with replaceAll!

  • 9 Mind-Blowing Canvas Demos

    The <canvas> component has been a revelation for the visible specialists amongst our ranks.  Canvas gives the means for unimaginable and environment friendly animations with the added bonus of no Flash; these builders can flash their superior JavaScript abilities as an alternative.  Listed below are 9 unbelievable canvas demos that…

  • Animating CSS3 Transforms with MooTools Fx
  • CSS Columns

    One main gripe that we have all the time had about CSS is that creating layouts appears to be harder than it must be. We have now, after all, tailored and mastered the methods for creating layouts, however there is not any shaking the sensation that there must be a…

  • Retrieve Your Gmail Emails Using PHP and IMAP

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments