JavaScript Program to Replace all the Instances of a Character in the String

In this example, you will learn to write a JavaScript program that will replace all the instances of a character in the string.

To understand this example, you should have the knowledge of the following JavaScript programming topics:


Example 1: Replace All Instances Of a Character using Regex

// program to replace all instances of a character in a string

const string = 'Learning JavaScript Program';

let result = string.replace(/a/g, "A");

console.log(result);

Output

LeArning JAvAScript ProgrAm

In the above example, the RegEx is used with the replace() method to replace all the instances of a character in a string.

/g represents that the operation is carried out for all in instances of a string.


Example 2: Replace All Instances Of Character using built-in Methods

// program to replace all instances of character in a string

const string = 'Learning JavaScript Program';

let splitString = string.split('a');

let result = splitString.join('A');

console.log(result);

Output

LeArning JAvAScript ProgrAm

In the above example, the built-in methods are used to replace all the occurrences of a character in a string.

The split('a') method splits the string into an array.

["Le", "rning J", "v", "Script Progr", "m"]

The join('A') method joins all the array elements into a string by adding A between each array element.

LeArning JAvAScript ProgrAm