Click to See Complete Forum and Search --> : Question for khalidali63


Webskater
01-16-2003, 03:42 PM
Hello
In your answer to a question about replacing quotation marks you included this code in your answer.

data = data.replace(/\"/g,"\\\"");
data = data.replace(/'/g,"\\'");

I wonder if you would be kind enough to explain exactly what is happening here and the significance of the forward and back(ward) slashes. Thanks.

khalidali63
01-16-2003, 04:10 PM
Originally posted by Webskater
Hello
data = data.replace(/\"/g,"\\\"");
data = data.replace(/'/g,"\\'");


I used a JavaScript String objects method replace().
the documented format is

replace(regexp, newSubStr)

first parameter is a regular expression,and the second is the value that needs to be replace with.
What I did here is as follows
data = data.replace(/\"/g,"\\\"");
replace all of the instances of the " (doueble qoutes) with \".
forward slashes are part of the regular expression syntax and back slashes are escape characters.

Khalid

Webskater
01-16-2003, 04:16 PM
Thanks Khalid for your reply. I am a little wiser but not much. I don't understand the concept of an escape character and do not understand the significance of the /g.
I realise I need to do some reading but I have searched the netscape.devedge (or whatever it is called) site but can't find any info. Any idea where I can find out all about this.
Thanks again

khalidali63
01-16-2003, 11:42 PM
You can begin here
http://developer.netscape.com/docs/manuals/js/client/jsref/string.htm#1194258
/g means globally,or asmany instances of the character before g.

search for regular expressionn on google..you'll find a bunch..

Khalid

Webskater
01-17-2003, 03:22 AM
Thanks again.