javascript 如何在javascript中转义所有单引号和双引号

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24485024/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 02:53:27  来源:igfitidea点击:

How to escape all single and double quotes in javascript

javascripthtmlstring

提问by kishore

I have a variable in my script

我的脚本中有一个变量

var st=""

in that quotes I have to give a string and this string contains lot of single and double quotations

在那个引号中,我必须给出一个字符串,这个字符串包含很多单引号和双引号

I think there is no problem with single quotations, but problem with the double quotes" only.

我认为单引号没有问题,但只有双引号有问题。”

In this I can't manually replace \"for all, even I tried with editor that "replace with \"but its not working

在此我无法手动替换\"所有内容,即使我尝试使用"替换为\"但它不起作用的编辑器

回答by vasa

You will need to use regular expression for this,

您将需要为此使用正则表达式,

st.replace(/"/g, '\"');

Checkout more on regexes here

此处查看有关正则表达式的更多信息

回答by DWX

Try this:-

试试这个:-

str.replace(/["']/g, "")