Replace double quote in javascript variable string

一笑奈何 提交于 2019-12-13 10:05:50

问题


How to replace all double quotes (") instance from a javascript variable string?

Here's my code

var double_quote_with_string = "Test "Double Quote" Test";
var replace_double_quote = double_quote_with_string.replace(/"/g, "\"");
alert(replace_double_quote);

I want the alert result should be - Test "Double Quote" Test

Here's the fiddle - https://jsfiddle.net/k1maf209/1/ (this is not working)

Any idea?


回答1:


When you try this,

var double_quote_with_string = "Test "Double Quote" Test";

this is syntactically incorrect. If you want quotes in your string try enclosing it in single quotes ',

var double_quote_with_string = 'Test "Double Quote" Test';

Or use escape character \

var double_quote_with_string = "Test \"Double Quote\" Test";



回答2:


var double_quote_with_string = 'Test "Double Quote" Test'; 
alert(double_quote_with_string );



回答3:


check this fiddle. I have replaced all the double quotes with single quotes.

edited fiddle

var double_quote_with_string = 'Test "Double Quote" Test';
var replace_double_quote = double_quote_with_string.replace(/"/g,       "'");
alert(replace_double_quote);


来源:https://stackoverflow.com/questions/33603539/replace-double-quote-in-javascript-variable-string

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