简体   繁体   English

在Javascript字符串中替换双引号和单引号

[英]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, "")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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