Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
664 views
in Technique[技术] by (71.8m points)

Replace double quote in javascript variable string

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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";

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...