I need to sanitize some chars. I have a solution that works but I am wondering if there is a better solution that may be faster or better or if I flat out should be approaching this differently?
function escapeStr(_str){
if (/\"|\'|\%/g.test(_str)) {
_str = _str.replace(/"/g, "%22");
_str = _str.replace(/'/g, "%27");
_str = _str.replace(/%/g, "%25");
}
return _str;
}
And vice versa:
function unescapeStr(_str){
if (/\%22|\%27|\%25/g.test(_str)) {
_str = _str.replace(/\%22/g, '"');
_str = _str.replace(/\%27/g, "'");
_str = _str.replace(/\%25/g, "%");
}
return _str;
}
replace
is undefined, it's because you trying to use it when on a_str
value that isn't a string. You can replace thetest
call withtypeof _str === 'string'
, and it should work the same. The problem is: Why is_str
sometimes not a string... Anyway, that's all irrelevant, since, as abl said, this sounds like a known problem \$\endgroup\$