Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replace Both Double and Single Quotes in Javascript String

I am pulling in some information from a database that contains dimensions with both ' and " to denote feet and inches. Those characters being in my string cause me problems later and I need to replace all of the single and double quotes. I can successfully get rid of one or the other by doing:

this.Vals.replace(/\'/g, "")   To get rid of single quotes 

or

this.Vals.replace(/\"/g, "")   To get rid of double quotes 

How do I get rid of both of these in the same string. I've tried just doing

this.Vals.replace(/\"'/g, "") 

and

this.Vals.replace(/\"\'/g, "") 

But then neither get replaced.

like image 521
jmease Avatar asked Oct 13 '11 20:10

jmease


People also ask

How do you replace a quote in JavaScript?

Use the String. replace() method to replace single with double quotes, e.g. const replaced = str. replace(/'/g, " ); . The replace method will return a new string where all occurrences of single quotes are replaced with double quotes.

Does JavaScript accept single and double quotes?

Both single (' ') and double (" ") quotes are used to represent a string in Javascript. Choosing a quoting style is up to you and there is no special semantics for one style over the other. Nevertheless, it is important to note that there is no type for a single character in javascript, everything is always a string!

How do you replace a double quote in a string?

To remove double quotes just from the beginning and end of the String, we can use a more specific regular expression: String result = input. replaceAll("^\"|\"$", ""); After executing this example, occurrences of double quotes at the beginning or at end of the String will be replaced by empty strings.

Can you use single quotes for strings in JavaScript?

Strings in JavaScript are contained within a pair of either single quotation marks '' or double quotation marks "". Both quotes represent Strings but be sure to choose one and STICK WITH IT. If you start with a single quote, you need to end with a single quote.


1 Answers

You don't escape quotes in regular expressions

this.Vals.replace(/["']/g, "") 
like image 66
Joe Avatar answered Sep 22 '22 10:09

Joe