简体   繁体   中英

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.

您不会在正则表达式中转义引号

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

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM