Page 1 of 1

How to perform this ??

Posted: Thu Jun 28, 2007 6:05 am
by PHPycho
Hello forums !!
case:
Suppose i had the following var
var text = "This is my text with 'quotes'";
I want to replace the ' -> \' using some function like replace()
How to perform this ?
Thanks in advance to all of you

Posted: Thu Jun 28, 2007 6:11 am
by patrikG
Do you want to do this in Javascript or PHP?

Posted: Thu Jun 28, 2007 6:38 am
by vigge89
patrikG wrote:Do you want to do this in Javascript or PHP?
Considering this is in the client side forum and the code looks like JS, I'd guess JavaScript ;)
I'm not very familiar with JS, but doesn't this work?

Code: Select all

var text = "This is my text with 'quotes'"; 
text.replace("'", "\'");

Posted: Thu Jun 28, 2007 5:56 pm
by JellyFish
What do you want to replace and with what? :?

Posted: Thu Jun 28, 2007 7:00 pm
by superdezign
JellyFish wrote:What do you want to replace and with what? :?
Single quote with escaped single quote. No idea why he wants to do it client-side though.

Posted: Thu Jun 28, 2007 7:12 pm
by JellyFish
superdezign wrote:No idea why he wants to do it client-side though.
Why would there be a reason?

Posted: Fri Jun 29, 2007 12:19 am
by PHPycho
Thanks Forums
I got the solution which is as:

Code: Select all

var text = "This is my text with 'quotes'";
text = text.replace(/\'/g, "\\\'");
Thanks for the help.