How to perform string replaceAll() in JS? [SOLVED]


JavaScript

Introduction to JS string replaceAll()

The JavaScript replaceAll method is a string method that allows you to search for a specified pattern in a string and replace it with another string. This method is similar to the replace method, but it allows you to replace all occurrences of the specified pattern instead of just the first one.

In this article, we will show different ways to use the replaceAll method in JavaScript.

 

Using replaceAll method with string argument

To use the replaceAll method, you must first create a string object and then call the replaceAll method on that object. The method takes two arguments: the pattern to search for and the string to replace it with.

let myString = "Hello, world!";
myString = myString.replaceAll("world", "JavaScript");
console.log(myString);

Output

Hello, JavaScript!

In this example, we create a string called myString and then use the replaceAll method to search for the word "world" and replace it with the word "JavaScript". The resulting string is then logged to the console.

Now, let’s show another example where we replace a string with another string. In this example, we use the replaceAll method to search for the letter "a" and replace it with the letter "an". This results in a string where all occurrences of the letter "a" are replaced with "an".

let myString = "Today is a beautiful day!";
myString = myString.replaceAll("a", "an");
console.log(myString);

Output

Today is an beanutiful dany!

 

Using replaceAll method with regex argument

Instead of using string to replace another string, we can make use of regex to search for characters that fit the pattern to be replaced.

In this example, we use the replaceAll method to search for any whitespace characters in the string and replace them with an empty string. This results in a string where all whitespace characters are removed.

let myString = "This is a test string.";
myString = myString.replaceAll(/\\s/g, "");
console.log(myString);

Output

Thisisateststring.

In another example, we can use the replaceAll method to search for the pattern "Hello" at the beginning of the string and replace it with the string "Goodbye". This results in a string where the first occurrence of "Hello" is replaced with "Goodbye".

let myString = "Hello, world! hello";
myString = myString.replaceAll(/^Hello/g, "Goodbye");
console.log(myString);

Output

Goodbye, world! hello

 

Summary

As you can see, the replaceAll method is a powerful tool for searching and replacing patterns in strings. It is similar to the replace method, but it allows you to replace all occurrences of the specified pattern instead of just the first one.

 

References

String.prototype.replaceAll() - JavaScript | MDN (mozilla.org)

 

Olorunfemi Akinlua

Olorunfemi Akinlua

He is boasting over five years of experience in JavaScript, specializing in technical content writing and UX design. With a keen focus on programming languages, he crafts compelling content and designs user-friendly interfaces to enhance digital experiences across various domains. You can connect with him on his LinkedIn profile.

Can't find what you're searching for? Let us assist you.

Enter your query below, and we'll provide instant results tailored to your needs.

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

Buy GoLinuxCloud a Coffee

For any other feedbacks or questions you can send mail to admin@golinuxcloud.com

Thank You for your support!!

Leave a Comment