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

时间:2022-09-15 13:07:12

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.

但后来都没有被取代。

4 个解决方案

#1


65  

You don't escape quotes in regular expressions

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

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

#2


6  

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

#3


4  

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

你不需要在里面逃脱它。你可以使用|用于分隔搜索的字符。

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

#4


3  

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

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

#1


65  

You don't escape quotes in regular expressions

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

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

#2


6  

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

#3


4  

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

你不需要在里面逃脱它。你可以使用|用于分隔搜索的字符。

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

#4


3  

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

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