But I need to escape them inside a function call since I do not know the values that will be passed (db variables I can't escape from the database).
Is there a function that allows me to do something like the following?
JSON.stringify(plainTextStr).replace(/&/, "&").replace(/"/g, """)
Escape the apostrophe with a backslash:
onclick="INSERT_PRODUCT('188267','WILL AND GRACE ','32311','L\'ANNIVERSARIO DI NOZZE ','20101113|04|18|','13/11/2010 0.00.00','CANALE 5 ',this);"
It's maybe not totally clear from the question, but assuming that all you want is to send this to a PHP script for storing in a database, you of course would ideally utilize PHP's various methods such as
stripslashes() -- but if you're really not trying to get too fancy, simply adding 1 slash in front of any single quote is enough to send a SQL query right into PHP from the client-side. It's not safe, but maybe not necessary either.
str.replace(/'/g, "\\'"); // escaping \ with \, so used 2x
does the trick., like for example in something like this:
var body = $('#body').val().replace(/'/g, "\\'"); myCustomSQLqueryFunction("UPDATE mytable SET `content`='"+ body +"';" );
MySQL will now store your
body like you see it in the form field.
This is how I do it, basically
var display = document.getElementById('output'); var str = 'class="whatever-foo__input" id="node-key"'; display.innerHTML = str.replace(/[\""]/g, '\\"'); //will return class=\"whatever-foo__input\" id=\"node-key\"
This function worked for me (it removes and restores the quote again): Guessing that the data to be sent is the value of an input element,
var Url = encodeURIComponent($('#userInput').val().replace("'","\\'"));
Then get the original text again:
var originalText = decodeURIComponent(Url);
var cmpdetail = cmpdetail.replace(/'/g, "\\'");
its working for me.
©2020 All rights reserved.