Replace Both Double and Single Quotes in Javascript String

JavascriptReplaceSpecial Characters

Javascript Problem Overview


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.

Javascript Solutions


Solution 1 - Javascript

You don't escape quotes in regular expressions

this.Vals.replace(/["']/g, "")

Solution 2 - Javascript

mystring = mystring.replace(/["']/g, "");

Solution 3 - Javascript

You don't need to escape it inside. You can use the | character to delimit searches.

"\"foo\"\'bar\'".replace(/("|')/g, "")

Solution 4 - Javascript

Try this.Vals.replace(/("|')/g, "")

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionjmeaseView Question on Stackoverflow
Solution 1 - JavascriptJoeView Answer on Stackoverflow
Solution 2 - JavascriptDannyView Answer on Stackoverflow
Solution 3 - JavascriptAlex TurpinView Answer on Stackoverflow
Solution 4 - JavascriptdeviousdodoView Answer on Stackoverflow