Remove apostrophe from a string
I am working inside a program that allows me reference database fields using syntax such as {Cli:FirstName}, this references the Client's first name. The program allows me to use HTML and java script to display a web page inside a window inside the program.
The problem I am having is that I have written a lot of pages and scripts to display the correct information, but I have found a problem. I have scripts that I pass the database field to a function such as
function a('{Cli:FirstName}'},'{Cli:LastName}'){
logic;
}
I pass it this way because the function is in an external .js file. The program I am using 开发者_运维百科does not allow me to reference the database field directly except in the .html file that is displaying in the window in the program. Therefore, every function that is in the .js has a similar parameter list. I can write a function in the header of the page and directly access the database field without passing the field as a parameter, but this causes other problems, and I moved almost all the scripts out due to speed and other limits in the program that is displaying the web page.
The problem occurs when I try to pass a database field that has an apostrophe in it to a function in the.js file. It crashes the function call, since the apostrophe is a string delimiter. Some of the functions are event driven, but some are also when the page loads. The order of operations is that the program does the database query first, and then renders the html following normal flow for web pages. When, I load a page and a field has an apostrophe in it, and that is in a function call, I get an error message. Example Name: is O’Malley would crash the script. I need to fix it so after the database query is complete, I can go through the fields that I use as parameters in the functions and escape the apostrophe /’ before I pass into a function.
My first thought is write a function on the html page, therefore I can access the database field directly and then escape the apostrophe, and then pass that value to the other functions. This way the apostrophe removal function would be generic enough to work for any other of the fields with apostrophes. I need help on how to write the function and then pass that functions result to another function.
function removeApostrophe (){
var firstName = ‘{CLI:FirstName}’; // sets the variable equal to the string in the database
// do this for each field I use as a parameter in other functions
}
Why not just use double quotes instead of single quotes
There is no simple solution if you don't have the ability to control how it appears on the HTML page. If you can you should use entity escaping. This is not entirely safe, but why not simply wrap the string in double quotation marks: "
This assumes you'll not see double quotation marks in your string.
精彩评论