Source:- Remove double quotes from string in JavaScript
For more questions and answers visit our website at frontend interview questions
Using String.prototype.replace() Method
One of the simplest ways to remove double quotes from a string is by using the replace()
method along with a regular expression to match double quotes.
const stringWithQuotes = '"Hello, World!"';
const stringWithoutQuotes = stringWithQuotes.replace(/"/g, '');
console.log(stringWithoutQuotes); // Output: Hello, World!
In this example, the regular expression /"/g
matches all occurrences of double quotes in the string, and replace()
removes them by replacing with an empty string.
Using String.prototype.split() and Array.prototype.join() Methods
Another approach is to split the string by double quotes and then join the resulting array elements without the quotes.
const stringWithQuotes = '"Hello, World!"';
const stringWithoutQuotes = stringWithQuotes.split('"').join('');
console.log(stringWithoutQuotes); // Output: Hello, World!
Here, split('"')
splits the string into an array of substrings using double quotes as the…