This article will discuss removing all the occurrences of a character from a javascript string using simple methods and example illustrations.
Table of Contents:-
- Delete all occurrences of a character in javascript string using replace() and RegEx
- Delete all occurrences of a character in javascript string using replaceAll()
- Delete all occurrences of a character in javascript string using split() and join()
Delete all occurrences of a character in javascript string using replace() and RegEx
The replace() method in javascript will look for a particular pattern in the calling string and replace it with a replacement.
The first argument: is the pattern to be searched for, like a string or a regular expression.
The second argument: is the replacement.
Example:-
Remove all occurrences of ‘-‘ from the string “Javascript- is -popular language”
Code:-
let dummyString = 'Javascript- is -popular language' dummyString = dummyString.replace(/-/g,'') console.log( dummyString )
Explanation:-
The regular expression /-/g is passed as the first argument in the replace() method where,
- / and / mark the beginning and end of the pattern
- – specifies to match the symbol (‘-‘) in the string
- g specifies to replace all occurrences.
The replacement passed as the second argument is (”), which means nothing.
Output:-
Javascript is popular language
Delete all occurrences of a character in javascript string using replaceAll()
The replaceAll() method in javascript replaces all the occurrences of a particular character or string in the calling string.
The first argument: is the character or the string to be searched within the calling string and replaced.
The second argument: is the replacement.
Example:-
Remove all occurrences of ‘-‘ from the string “Javascript- is -popular language”
Code:-
let dummyString = 'Javascript- is -popular language' dummyString = dummyString.replaceAll('-', '') console.log( dummyString )
Explanation:-
Here in the above code, the replaceAll() method is used to find the symbol (‘-‘) and replace all its occurrences with (”) passed as the second argument.
Output:-
Javascript is popular language
Javascript remove double quotes from a string using split() and join()
Javascript’s split() method returns an array of substrings formed by splitting a given string.
Javascript’s join() method joins the elements of the array back into a string.
Example:-
Remove all occurrences of ‘-‘ from the string “Javascript- is -popular language”
Code:-
let dummyString = 'Javascript- is -popular language' dummyString = dummyString.split('-').join('') console.log( dummyString )
Explanation:-
The above code uses the split() method to split the original string into an array of substrings. The division is done based on a character (-). Finally, using the join() method, these array elements are joined back to form a string. In the resulting string, all occurrences of character (-) are removed.
Output:-
This-Is-DummyString
Read More:
- Remove character at a specific index from a string in javascript
- Javascript: How to remove text from string
- Javascript: Remove last character of string
- Javascript: Remove first character from a string
I hope this article helped you to delete all occurrences of a particular character from a string in javascript. Good Luck !!!