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 certainly one of my most learn articles.
The confusion lies in that exchange
solely replaces the primary prevalence of a substring, not all occurrences. For instance:
'yayayayayaya'.exchange('ya', 'na'); // nayayayayaya
To exchange all cases of a substring, you have wanted to make use of a daily expression:
'yayayayayaya'.exchange(/ya/g, 'na'); // nananananana
Utilizing common expressions is actually highly effective however let’s be sincere — oftentimes we merely wish to exchange all cases of a easy substring that should not require a daily expression.
Fortunately, this 12 months the JavaScript language supplied us with String.prototype.replaceAll
, a technique for changing with out utilizing common expressions:
'yayayayayaya'.replaceAll('ya', 'na'); // nananananana
Typically 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
!
Digicam and Video Management with HTML5
Shopper-side APIs on cell and desktop gadgets are shortly offering the identical APIs. After all our cell gadgets bought entry to a few of these APIs first, however these APIs are slowly making their strategy to the desktop. A type of APIs is the getUserMedia API…
Responsive and Infinitely Scalable JS Animations
Again in late 2012 it was not simple to search out open supply tasks utilizing
requestAnimationFrame()
– that is the hook that permits Javascript code to synchronize with an online browser’s native paint loop. Animations utilizing this technique can run at 60 fps and ship implausible…
Get Slick with MooTools Kwicks
After I first noticed MooTools graphical navigation, I used to be impressed. I assumed it was a quite simple but inventive means of utilizing Flash. After I right-clicked and noticed that it was JavaScript, I used to be floored. How might they obtain such…
Source link