Replace double quote in javascript variable string

2019-09-22 07:12发布

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?

3条回答
Bombasti
2楼-- · 2019-09-22 07:17

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";
查看更多
不美不萌又怎样
3楼-- · 2019-09-22 07:17
var double_quote_with_string = 'Test "Double Quote" Test'; 
alert(double_quote_with_string );
查看更多
Root(大扎)
4楼-- · 2019-09-22 07:36

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);
查看更多
登录 后发表回答