Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Javascript equivalent of PHP's addslashes() ?

Status
Not open for further replies.

Dweezel

Technical User
Feb 12, 2004
428
GB
I'm guessing there is a javascript equivalent to PHP's addslashes. If anyone could let me no I'd be grateful.

Also are there any resources on the net that show comparisons between the functions of languages such as Javascript and PHP. By that I mean showing a function in one language and then showing the equivalent in another. That would be very useful to me for things like string functions. If you no of something similar out there could you post up a link.

Cheers
 
no direct equivalent, what are you trying to do? pass a php variable to javascript? in javascipt also \ is used for escaping...

Known is handfull, Unknown is worldfull
 
You could put this code in your own addslashes function if you don't wanna type the replaces every time.
Code:
str = str.replace(/\'/g, "\\\'").replace(/\"/g, "\\\"");
As for the link, the best thing I can tell you is to google for javascript string functions. You also might wanna take a look at regular expressions.

-kaht

Weaseling out of things is important to learn. It's what separates us from the animals... except the weasel. - Homer Simpson (no, I'm not Homer)
 
>>You could put this code in your own addslashes function if you don't wanna type the replaces every time

i thought about replace function but kaht how is he gonna get the value into javascript? it its from a textfield then it will automatically get escaped, it its a harcoded string then it will have to have the \ character.

i couldnt think of a situation when \' is used in javascript...

Known is handfull, Unknown is worldfull
 
how is he gonna get the value into javascript?

*shrug* He only asked for a javascript equivalent to the function, and using the replace methods is exactly what that is.

-kaht

Weaseling out of things is important to learn. It's what separates us from the animals... except the weasel. - Homer Simpson (no, I'm not Homer)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top