Saturday, December 21, 2024
HomeSoftware DevelopmentJavaScript String replaceAll

JavaScript String replaceAll

[ad_1]

Changing a substring of textual content inside a bigger string has all the time been deceptive in JavaScript. I wrote Change All Occurrences of a String in JavaScript years in the past and it is nonetheless considered 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’ve got wanted to make use of an everyday expression:

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

Utilizing common expressions is definitely highly effective however let’s be trustworthy — oftentimes we merely need to exchange all cases of a easy substring that should not require an everyday expression.

Fortunately, this yr the JavaScript language supplied us with String.prototype.replaceAll, a way 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!

  • fetch API

    One of many worst saved secrets and techniques about AJAX on the net is that the underlying API for it, XMLHttpRequest, wasn’t actually made for what we have been utilizing it for.  We have carried out properly to create elegant APIs round XHR however we all know we are able to do higher.  Our effort to…

  • CSS Filters

    CSS filter assist just lately landed inside WebKit nightlies. CSS filters present a way for modifying the rendering of a fundamental DOM ingredient, picture, or video. CSS filters enable for blurring, warping, and modifying the colour depth of parts. Let’s have…

  • jQuery Wookmark

    The very first thing that hits you once you go to Pinterest is “Whoa, the columns are the identical width and the pictures are minimize to suit simply the best way they need to.”  Fundamental internet customers most likely suppose nothing of it however as a developer, I can admire the…

  • jQuery Random Link Color Animations

    Everyone knows that we are able to set a hyperlink’s :hover shade, however what if we need to add a bit extra dynamism and aptitude? jQuery means that you can not solely animate to a specified shade, but in addition means that you can animate to a random shade. The…


[ad_2]
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments