Replace double quotes in a string

  • Thread starter Christopher Benson-Manica
  • Start date
C

Christopher Benson-Manica

Why doesn't this replace all double quotes in a string with the letter
Q?

source.replace( /"([^"]*)"/g, "Q$1Q" );
 
M

Michael Winter

Why doesn't this replace all double quotes in a string with the letter Q?

source.replace( /"([^"]*)"/g, "Q$1Q" );

It does for me. Are you forgetting to reassign the result to 'source'?

Mike
 
I

Ivo

Why doesn't this replace all double quotes in a string with the
letter Q?

source.replace( /"([^"]*)"/g, "Q$1Q" );

Because your regex looks for strings with at least two quotes, possibly
separated by other characters.
To replace all double quotes, no matter what, the regex is at its simplest:

source.replace( /"/g, 'Q' );

HTH
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top