While working in javascript, sometimes we need to remove double quotes (“) from the start and end. This article will discuss removing double quotes from a javascript string at the first and last position using simple methods and example illustrations.
Table of Contents:-
- Javascript remove double quotes from start and end of a string using replace()
- Javascript remove double quotes from start and end of a string using substring()
- Javascript remove double quotes from start and end of a string using slice()
Javascript remove double quotes from start and end of a string using replace()
Javascript’s replace() method searches for a particular pattern (passed as the first argument) in the original string and replaces it with a replacement (passed as the second argument).
Example:-
Remove the double quotes(“) from the start and end of the string “Hello Javascript- “This language” is very popular.”
Code:-
let dummyString = '"Hello Javascript- "This language" is very popular."' dummyString = dummyString.replace(/(^"|"$)/g, '') console.log(dummyString)
Explanation:-
Frequently Asked:
- Javascript: String Replace Space with Dash
- Remove comma from string in javascript
- Delete all occurrences of a character in javascript string
- JavaScript: Convert ASCII Code to Characters
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 at the beginning of the string.
- “$ specifies to match double quotes at the end of the string.
- g specifies to replace all occurrences.
The replacement passed as the second argument is (”) nothing.
Output:-
Hello Javascript- "This language" is very popular.
Javascript remove double quotes from start and end of a string using substring()
Javascript’s substring() method returns a subset of the string between the start and end indexes or towards the end of the string. The index is zero-based.
Example:-
Remove the double quotes(“) from the start and end of the string “Hello Javascript- “This language” is very popular.”
Code:-
let dummyString = '"Hello Javascript- "This language" is very popular."' if (dummyString.length >= 2 && dummyString.charAt(0) == '"' && dummyString.charAt(dummyString.length - 1) == '"') { dummyString = dummyString.substring(1, dummyString.length - 1) } console.log(dummyString)
Explanation:-
Here, the above code checks if the length of the dummyString is greater than 2 and if the character at 0th position and the last character of the string is a double-quote (“). If yes, then extract a substring from dummyString starting after the first double quote and ending one character before double quote. Finally, assign this value back to dummyString.
Note that if any of the double quotes are missing, this solution will not work and should be used when one is sure that double quotes are present at both places -> at the start and end of the string.
Output:-
Hello Javascript- "This language" is very popular.
Javascript remove double quotes from start and end of a string using slice()
The slice(beginIndex, endIndex) method of javascript returns a copy as a new string. The beginIndex and endIndex describe from where the extraction needs to be started and ended.Â
If any of the indexes are negative, it is considered -> string.length – index.
Example:-
Remove the double quotes(“) from the start and end of the string “Hello Javascript- “This language” is very popular.”
Code:-
let dummyString = '"Hello Javascript- "This language" is very popular."' if (dummyString.length >= 2 && dummyString.charAt(0) == '"' && dummyString.charAt(dummyString.length - 1) == '"') { dummyString = dummyString.slice(1, dummyString.length - 1) } console.log(dummyString)
Explanation:-
The working of the above code uses the slice() method and is similar to the one written with the substring() method.
Note that if any of the double quotes are missing, this solution will not work and should be used when one is sure that double quotes are present at both places -> at the start and end of the string.
Output:-
Hello Javascript- "This language" is very popular.
Read More:
- Remove double quotes from a string in javascript (4 ways)
- 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 delete double quotes from the first and last place of the string in javascript. Good Luck!!!