Replace all Instances of a Character in a String in JavaScript

Replace all Instances of a Character in a String in JavaScript

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

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

JavaScript String

JavaScript Regex

JavaScript String split()

JavaScript Array join()


Example 1: Replace All Instances Of a Character Using Regex



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 the string.


Example 2: Replace All Instances Of Character Using Built-in Methods

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

Post a Comment

Previous Post Next Post