In this post, we will see how to remove double quotes from String in JavaScript.
Ways to Remove Double Quotes from String in JavaScript
There are multiple ways to do it. Let’s go through them.
Table of Contents
Using replace()
To remove double quotes from String in JavaScript:
- Use
replace()
method with regular expression/"/g
as first parameter and empty string as second parameter.
1 2 3 4 5 |
var str='Welcome to "Hello World"'; str=str.replace(/"/g,''); console.log(str); |
Output:
1 2 3 |
Welcome to Hello World |
The replace()
method returns a new string with matches of pattern replaced by a replacement string.
It takes 2 parameters.
- Regular expression which we want to match in String.
- Replacement String – String with which we want to replace matched string.
Let’s go through regular expression /"/g
used in above replace method.
- The two forward slashes mark start and end of regular expression
"
to match double quotes in the string.g
denotes global replacement here. It is flag at end of regex which depicts that we want to remove all the double quotes not only first one.
replace()
returns a new String, it does not change contents of original String.
Using replaceAll()
To remove comma from string in JavaScript:
- Use
replaceAll()
method with double quotes("
) as first parameter and empty string as second parameter.replaceAll()
method returns new String with all matches replaced by given replacement.
The replaceAll()
method returns a new string with matches of passed string replaced by a replacement string.
It takes 2 parameters.
- substring which we want to match in String.
- Replacement String – String with which we want to replace matched string.
1 2 3 4 5 |
var str='Welcome to "Hello World"'; str=str.replaceAll('"',''); console.log(str); |
Output:
1 2 3 |
Welcome to Hello World |
replaceAll()
method is not supported in Internet Explorer versions 6-11. Use replace method in case you are using them.
Using split() and join() method
To remove double quotes from String in JavaScript:
- Split the string by double quotes using String’s split() method.
- Join the String with empty String using Array’s join() method.
This method is useful, when you want to avoid regular expressions in replace()
method.
1 2 3 4 5 |
var str='Welcome to "Hello World"'; str=str.split('"').join(''); console.log(str); |
Output:
1 2 3 |
Welcome to Hello World |
We used split()
method to split the String on each occurrence of double quotes("
) into array.
Then joined the array elements with empty String using join()
method.
Using for loop
To remove double quotes from String in JavaScript:
- Iterate over string using for loop
- In each iteration, if character is not
"
, we include it instrWithoutQuotes
- If character is double quote
"
, then we do nothing and continue the loop.
1 2 3 4 5 6 7 8 9 10 11 |
var str='Welcome to "Hello World"'; var strWithoutQuotes = ''; for(let i =0; i<str.length; i++){ if(str.charAt(i) != '"'){ strWithoutQuotes += str.charAt(i); } } console.log("string without quotes: " +strWithoutQuotes) |
Output:
1 2 3 |
string without quotes: Welcome to Hello World |
Conclusion
We discussed different methods to remove double quotes in String in JavaScript. First two method i.e. remove()
and removeAll()
work very similar.
Third approach makes use of split()
and join()
concept which is often used to remove character from String. Idea is to split string by double quotes and join by empty string.
Final approach uses for loop to remove double quotes from String. I would avoid this approach as it involves boiler plate code.
That’s all about how to remove double quotes from string in JavaScript.