How to perform this ??

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
User avatar
PHPycho
Forum Contributor
Posts: 336
Joined: Fri Jan 06, 2006 12:37 pm

How to perform this ??

Post 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
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post by patrikG »

Do you want to do this in Javascript or PHP?
User avatar
vigge89
Forum Regular
Posts: 875
Joined: Wed Jul 30, 2003 3:29 am
Location: Sweden

Post 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("'", "\'");
User avatar
JellyFish
DevNet Resident
Posts: 1361
Joined: Tue Feb 14, 2006 7:18 pm
Location: San Diego, CA

Post by JellyFish »

What do you want to replace and with what? :?
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post 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.
User avatar
JellyFish
DevNet Resident
Posts: 1361
Joined: Tue Feb 14, 2006 7:18 pm
Location: San Diego, CA

Post by JellyFish »

superdezign wrote:No idea why he wants to do it client-side though.
Why would there be a reason?
User avatar
PHPycho
Forum Contributor
Posts: 336
Joined: Fri Jan 06, 2006 12:37 pm

Post 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.
Post Reply