PDA

View Full Version : Can someone suggest a good coding in Javascript



Basavaraj
Mar 17th, 2007, 09:03 AM
Hi,

I am passing string varible in a single quote but if the string contain single quote or double quote before ending the string it will terminate and gives javascript error

ex: var="<%=abc%>";

abc contain "java"scri'pt";

so i want to ignore both double and single quotes within the string please help me

one more thing
replace(" ' ", " \' ") and relace(' " ', ' \" ') will not work


Thanks
Basavaraj

Philip M
Mar 17th, 2007, 09:36 AM
Get rid of all the quotes:-

tempStr=tempStr.replace(/\"|\'/g, "")

and if required add single quotes to start and end of the string

tempStr=tempStr.replace(/^(.+)$/, "'$1'")

Also try this:-

var tempStr = "philip's"
tempStr=tempStr.replace(/\"|\'/g, "\\'")
alert (tempStr);
tempStr=tempStr.replace(/^(.+)$/, "'$1'")
alert (tempStr);

but you cannot have both " and ' in the same string.

Basavaraj
Mar 20th, 2007, 08:27 AM
var val1= 'De've"n Doshi' ;

This should be print as 'Deven Doshi' can you help me out how to do in java script