Replace Both Double and Single Quotes in Javascript String

微笑、不失礼 提交于 2019-12-20 10:29:35

问题


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.


回答1:


You don't escape quotes in regular expressions

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



回答2:


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



回答3:


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

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



回答4:


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



来源:https://stackoverflow.com/questions/7760262/replace-both-double-and-single-quotes-in-javascript-string

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!