You are currently viewing 3 Great Ways to Remove Double Quotes from String in JS

3 Great Ways to Remove Double Quotes from String in JS

JavaScript Remove Quotes from String

When we are working with data from an external source that contains quotes, or when formatting output that should not include quotes. At that time, we have to remove the quotes from the string. So, In this article, we will explore several methods to remove double quotes from string in javascript.

Why should we remove quotes from a string?

  • When displaying data on a web page, you may want to format it in a certain way without including quotes.
  • When we compare two strings, it’s important to ensure that they are formatted. If one string has quotes and the other doesn’t, the comparison may not yield the desired result.
  • In some cases, allowing quotes in a string can pose a security risk, such as when dealing with user input that is used in SQL queries.
  • When working with data from an external source, such as a JSON API, the data may be formatted with quotes. At that time, we have to remove the quotes from the string.

How to remove quotes from a string?

Follow the below methods to remove the double quotes from the string:

1. Using the replace() method:

In this method, we use the regular expression (/”/g) to match all occurrences of double quotes and replace them with an empty string:

let str = 'I drink "coffee" every morning';
document.write(str);
let newStr = str.replace(/"/g, ' ');
document.write(newStr);

Output:

javascript remove quotes from string

  • In this method, we have created the variable ‘str‘ and assigned it to some text. Note that the word ‘coffee‘ is enclosed in double quotes.
  • To remove the double quotes in the word ‘coffee’, we need to find double quotes using regular expressions like (/”/g) and replace empty strings using replace() method like (‘ ‘).
  • And we store the value returned by the replace() method in a variable called ‘newStr’. So, the variable ‘newStr’ contains an unquoted string in ‘str’ variable.
  • Finally, we print the ‘newStr’ variable using the document.write(). See the output and understand the difference.
READ ALSO  Delete Operator in JavaScript | Explained

2. Using the split() and join() methods:

In this method, we split the string into an array of substrings based on the quote character, and then join the array back into a string without the quotes.

let str = 'I drink "coffee" every morning';
let arr = str.split('"');
let newStr = arr.join('');
console.log(newStr);

//Output: I drink coffee every morning

In this example:

  • We define a string that contains double quotes.
  • Then we use the split() method to split the string into an array of substrings based on the double quote character and stored the value in the variable ‘arr‘.
  • And then use the join() method to join the array back into a string without the quotes and stored the value in the variable ‘newStr
  • Finally, we log the variable ‘newStr’ using console.log() method. So, the variable contains quotes removed string ‘str’.

3. Using the slice() method:

Use this method only if you want to remove double quotes from a word like “Hello”, “Ride”, “Fruits”, etc. Because in this method we are going to remove only the first and last character. So, we’re going to use slice() method to remove double quotes from the first and last position from the string. For example:

let str = '"coffee"';
if (str.charAt(0) === '"' && str.charAt(str.length - 1) === '"') {
str = str.slice(1, -1);
}
console.log(str);

// Output: coffee

In this example:

  • We have created the variable ‘str’ and assigned it to the word with double quotes.
  • Then we set the condition if the first character and last character of a string contain double quotes using the if statement. If it is, the if statement will run otherwise not.
  • Inside the statement, we assigned to slice() method for the variable ‘str’. Then we passed two arguments to the slice() method, the first is ‘1‘ and the second is ‘-1‘.
    1: This means that the extraction of the new string will start from the second character of the original string (str). This is because JavaScript uses a zero-based index, so the first character has an index of 0.
    -1: This means that the extraction will end at the second to last character of the original string. The -1 index refers to the last character of the string, but since it’s negative, it starts counting from the end of the string. Therefore, -1 refers to the second to last character of the string.
  • Finally, log that variable ‘str’.

Leave a Reply