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;
}
2 Answers 2
Instead of hardcoding the characters, and their escape values specifically in your .replace
call, you can pass a callback function for the replacement. I suggested that you dive into encodeURI
:
So, something like:
function escapeStr(_str) {
return _str.replace(/([%'"])/g, encodeURI)
}
function unescapeStr(_str) {
return _str.replace(/(%(?:2[257]))/g, decodeURI)
}
const e = escapeStr, u = unescapeStr;
console.log( e("string") );
console.log( e("some\" char%s") );
console.log( u("string") );
console.log( u("some%22 char%25s") );
In first and second function
function escapeStr(_str){
if (/\"|\'|\%/g.test(_str)) { // unnecessary if
_str = _str.replace(/"/g, "%22"); // unnecessary asigment
_str = _str.replace(/'/g, "%27"); // unnecessary asigment
_str = _str.replace(/%/g, "%25"); // unnecessary asigment
}
return _str;
}
You can add if (value) this will prevent errors while passing null of something like this
This probably more complex and optimal:
const escapeStr = str => str ? str
.replace(/%/g, "%25")
.replace(/"/g, "%22")
.replace(/'/g, "%27") : str
const unescapeStr = str => str ? str
.replace(/\%25/g, "%")
.replace(/\%22/g, '"')
.replace(/\%27/g, "'") : str
And if we want to go crazy use this:
const escapeStr = str => str ? str.replace(/[%'"]/g, encodeURI) : str
const unescapeStr = str => str ? str.replace(/\(%2[257])/g, decodeURI) : str
const testData = [`"hello"`,`'world'`,`2%3`,``, `xyz`, `%22hello%22`]
for (const data of testData) {
console.log(`${data}:`)
console.log(`escapeStr -> ${escapeStr(data)}`)
console.log(`escapeStr -> ${unescapeStr(data)}`)
}
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\$