I have a javascript variable which hold the value taken from somewhere else(lets say from a API call), taken string is given bellow,
He's the reason for this
I assign this string to a variable name ‘sample’. But when I print it, it doesn’t work since the string has ” ‘ ” character. I want to add ” \ ” before the ” ‘ ” character. I tried using this,
var sample = (get the string from a api call).replace(/'/g,"\\\'");
But it doesn’t work?
Use
encodeURIComponentto escape a string for inserting into a URI.If you are printing it into HTML then use
htmlspecialcharsto make it safe.If you are printing it into JavaScript then use
json_encodeto make it safe.