While working in javascript, sometimes we need to remove double quotes (“) from a string. This article will discuss removing double quotes from a string in javascript using different methods and example illustrations.

Table of Contents:-

Javascript remove double quotes from a string using replace()

We will be using the replace() method of javascript along with RegExp to replace the occurrences of double quotes (“) with (”).

The replace() method in javascript looks for a particular pattern in the provided string and replaces it with a replacement. The pattern for search is passed as the first argument and can be a regular expression or a function. The replacement is passed as the second argument

Example:-

Remove all the double quotes(“) from the string “Hello Javascript- “This language” is very popular.”

Code:-

let dummyString = 'Hello Javascript- "This language" is very popular."'
let finalString = dummyString.replace(/["]+/g, '')
console.log("original string: " + dummyString)
console.log("final string: " + finalString)

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 double quotes in the string
  • + specifies one or more character
  • g specifies to replace all occurrences.

The replacement passed as the second argument is (”) nothing.

Output:-

original string: Hello Javascript- "This language" is very popular."
final string: Hello Javascript- This language is very popular.

Javascript remove double quotes from a string using replaceAll()

We will be using the replaceAll() method of javascript to replace all the occurrences of double quotes (“) with (”).

The replaceAll() method in javascript looks for a particular character/string in the original string and replaces it with a replacement. The character/string to be searched for is passed as the first argument. The replacement is passed as the second argument.

Example:-

Remove all the double quotes(“) from the string “Hello Javascript- “This language” is very popular.”

Code:-

let dummyString = 'Hello Javascript- "This language" is very popular."'
let finalString = dummyString.replaceAll('"', '')
console.log("original string: " + dummyString)
console.log("final string: " + finalString)

Explanation:-

The replaceAll() method is used to find the double quotes (“) passed in as the first argument and replace all its occurrences with (”) passed as the second argument.

Output:-

original string: Hello Javascript- "This language" is very popular."
final string: Hello Javascript- This language is very popular.

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 the double quotes(“) from the string “Hello Javascript- “This language” is very popular.”

Code:-

let dummyString = 'Hello Javascript- "This language" is very popular."'
let finalString = dummyString.split('"').join('')
console.log("original string: " + dummyString)
console.log("final string: " + finalString)

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 double quote character (“). Finally, using the join() method, these array elements are joined back to form a string without double-quotes.

Output:-

original string: Hello Javascript- "This language" is very popular."
final string: Hello Javascript- This language is very popular.

Javascript remove double quotes from a string using a custom method

We will be writing a custom method to explicitly look for double quotes(“) within a string and remove it.

Example:-

Remove all the double quotes(“) from the string “Hello Javascript- “This language” is very popular.”

Code:-

let dummyString = 'Hello Javascript- "This language" is very popular."'
let finalString = '';
for(let i =0; i<dummyString.length; i++){
    if(dummyString.charAt(i) != '"'){
        finalString += dummyString.charAt(i);
    }
}
console.log("string without quotes: " + finalString)

Explanation:-

In the above code, we are iterating each character of the original string using a for loop. If a double quote (“) is found, then do not involve it in creating another string -> finalString formed with all the other characters of the original string. The variable finalString does not have double-quotes.

Output:-

string without quotes: Hello Javascript- This language is very popular.

Read More:

I hope this article helped you to delete double quotes from a string in javascript. Good Luck !!!