简体   繁体   中英

Process ASCII codes present in string

Ok. This may be a dumb question but I am stuck.

In my javascript, I have a string variable that contains ' which stands for single quote. eg some_text_'_some_text Now, I want to replace this with the actual single quote like some_text_'_some_text .

Obvious way would be using str.replace(/'/g,"'") but the problem is I write this javascript code into a third party software that replaces ' by ' when I save the script. So, if I open script again, it shows str.replace(/'/g,"'") . So when the script runs, it does not do replace operation correctly.

One would ask why do I need this replace to work?
The reason is that this variable is passed on to build a SQL query and I don't want ' in my query. I want it to be ' instead which I can escape in SQL.

EDIT
So, I realized the reason for this behavior and potential answerers may want to take this into account. The software I work with stores all its files as XML including javascript code I write. So, it converts all special characters to HTML codes while saving and parses them back when it reads it. That's the reason ' gets converted to ' .

If @Bergi's suggestion doesn't help, try to trick the software by something like

str.replace(new RegExp("&"+"#"+"x27;", "g"), "'")

– basically splitting the numeric character reference into several pieces, so that the software that is messing with things can't recognize it as such any more.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM