2011-10-13 230 views
31

我从包含“and”尺寸的数据库中提取一些信息来表示英尺和英寸。这些字符在我的字符串中导致我稍后出现问题,并且需要替换。所有的单引号和双引号的,我可以顺利拿到做掉一个或另一个的:在Javascript字符串中替换双引号和单引号

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

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

我如何才能在相同的字符串摆脱这两种的。我刚刚试过

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

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

但后来没有被替换。

+0

这两种方法的工作就像一个魅力。我会尽快给我一个答案。谢谢! – jmease

回答

60

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

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

您不必里面逃脱它。您可以使用|字符来分隔搜索。

"\"foo\"\'bar\'".replace(/("|')/g, "") 
相关问题