How to Replace All Occurrences of a String

Introduction

One of the main data types in JavaScript is strings. These data types are used for storing and manipulating text. And luckily for us, there are quite a few methods that help us in manipulating these data types.

These methods ar…


This content originally appeared on DEV Community and was authored by Boyan Iliev

Introduction

One of the main data types in JavaScript is strings. These data types are used for storing and manipulating text. And luckily for us, there are quite a few methods that help us in manipulating these data types.

These methods are one of the many built-in methods of JavaScript. This is because JS has some built-in objects. Some of them are: Array, Math and String. And for each object, some properties and methods are associated with them.

In this post we are going to see How to Replace All Occurrences of a String with the replaceAll() method.

replaceAll() Method

In order to use this method, we first have to create our string. Let's add randomly ! inside this strong so that we can remove it.

let intro = 'My! n!ame! i!s Bo!yan!';

Now to remove it, we have to write the strings name, then add the replaceAll method next to it, and pass in the first argument which element we want to be replaced or removed. An important thing is to remember that you have to save the new string into a new variable. It should look something like this:

let newIntro = intro.replaceAll('!', '')
console.log(newIntro);

<- My name is Boyan

Now if you want to replace the elements, just pass in the second argument as what you want it replaced with. Let's replace the whitespace with -.

let newerIntro =newIntro.replaceAll(' ', '-');
console.log(newerIntro)

<- My-name-is-Boyan

Conclusion

This is pretty much it to the replaceAll() method. It is the main method used, if not the only method, that replaces all occurrences of a string.

I hope that this post has helped you and I would love to hear some feedback in the comments below.


This content originally appeared on DEV Community and was authored by Boyan Iliev


Print Share Comment Cite Upload Translate Updates
APA

Boyan Iliev | Sciencx (2021-07-08T17:33:02+00:00) How to Replace All Occurrences of a String. Retrieved from https://www.scien.cx/2021/07/08/how-to-replace-all-occurrences-of-a-string/

MLA
" » How to Replace All Occurrences of a String." Boyan Iliev | Sciencx - Thursday July 8, 2021, https://www.scien.cx/2021/07/08/how-to-replace-all-occurrences-of-a-string/
HARVARD
Boyan Iliev | Sciencx Thursday July 8, 2021 » How to Replace All Occurrences of a String., viewed ,<https://www.scien.cx/2021/07/08/how-to-replace-all-occurrences-of-a-string/>
VANCOUVER
Boyan Iliev | Sciencx - » How to Replace All Occurrences of a String. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/07/08/how-to-replace-all-occurrences-of-a-string/
CHICAGO
" » How to Replace All Occurrences of a String." Boyan Iliev | Sciencx - Accessed . https://www.scien.cx/2021/07/08/how-to-replace-all-occurrences-of-a-string/
IEEE
" » How to Replace All Occurrences of a String." Boyan Iliev | Sciencx [Online]. Available: https://www.scien.cx/2021/07/08/how-to-replace-all-occurrences-of-a-string/. [Accessed: ]
rf:citation
» How to Replace All Occurrences of a String | Boyan Iliev | Sciencx | https://www.scien.cx/2021/07/08/how-to-replace-all-occurrences-of-a-string/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.