ReplaceAll for JavaScript

Unlike java code, JavaScript doesn’t have an exclusive “replaceAll” function. But that doesn’t mean the functionality’s absent in javaScript.

To replace all the occurrences of a particular character or string in a given string we make use of the letter ‘g’ which implies to replace all the occurrences of the given letters.

Ex. var stringToPerformReplace = “3+5-1+2″;

var replacedString = stringToPerformReplace.replace(“+”,”-”,”g”);

The new String will be “3-5-1-2″.

In a similar manner instead of “g”, if you specify “i” it implies “ignorecase”.

Note: You can club both together i.e. “gi” is valid.

Tags: , , , ,

Leave a comment